Rob
Rob

Reputation: 305

htaccess: how to rewrite specific GET vars?

For a main menu I've got about 3 header catagoty's I like to static rewrite them.

This is the code i'm using now:

RewriteCond %{QUERY_STRING} !dep= [NC]    
RewriteRule ^([0-9a-zA-Z-]+)/?$ index.php?dep=$1 [NC,L,QSA]   

www.hello.com/car/ will give the //$_GET var //?dep=car

simple how would I let: www.hello.com/car/ford/
let 'return' ?dep=car&id=ford

Now let say I know the 3 '?dep' paramters I'm using. How could you write:

If www.hello.com/? == car || boat || train  /  
rewrite to ?dep=  
else
rewrite to ?id= 

/*
In this example giving www.hello.com/ford/  
would return id=ford     
but, www.hello.com/boat/  
would return dep=boat 
*/

Upvotes: 0

Views: 65

Answers (1)

Jon Lin
Jon Lin

Reputation: 143886

simple how would I let: www.hello.com/car/ford/ let 'return' ?dep=car&id=ford

Add this rule after your rules:

RewriteRule ^([0-9a-zA-Z-]+)/([0-9a-zA-Z-]+)/?$ index.php?dep=$1&id=$2 [NC,L,QSA]   

Now let say I know the 3 '?dep' paramters I'm using. How could you write:

You'll need to change your conditions for this because you need to check for "id". Then the rule would match "car", "boat", or "train" specifically:

RewriteCond %{QUERY_STRING} !(id|dep)= [NC]    
RewriteRule ^(car|boat|train)/?$ index.php?dep=$1 [NC,L,QSA]

And add a second rule for the rest:

RewriteCond %{QUERY_STRING} !(id|dep)= [NC]    
RewriteRule ^([0-9a-zA-Z-]+)/?$ index.php?id=$1 [NC,L,QSA]

Upvotes: 2

Related Questions