Debrian
Debrian

Reputation: 53

Trouble with mod_rewrite and 2 parameters

I would like to use URL's on a website in the form www.x.com/cat/prod

First i would like to have www.x.com/cat/prod pass to www.x.com/index.php?cat=cat&prod=prod and i managed with this:

RewriteCond %{REQUEST_FILENAME} !-s 
RewriteCond %{REQUEST_FILENAME} !-l 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !\.(js|ico|gif|jpg|png|css|html|swf)$
RewriteRule ^(.*)/(.*)/?$ index.php?cat=$1&prod=$2 [B,QSA,L]

Secondly i would like to have it so if someone accessed www.x.com/index.php?cat=cat&prod=pro it should get 301 redirected to www.x.com/cat/prod and i used this:

RewriteCond %{THE_REQUEST} ^GET\s/+index\.php [NC]
RewriteCond %{QUERY_STRING} (^|&|\?)cat=(.*)&prod=(.*)($) [NC]
RewriteRule . /%2/%3? [R=301,L,NC]

The problem is it gives me www.x.com/cat&prod=prod instead of www.x.com/cat/prod, i tried a lot of things i found around here but i still can't manage to get it right.

Any advice is appreciated.

Upvotes: 0

Views: 41

Answers (1)

anubhava
anubhava

Reputation: 784918

Have it like this:

RewriteEngine On

RewriteCond %{THE_REQUEST} \s/+index\.php\?cat=([^&]+)&prod=([^&\s]+) [NC]
RewriteRule ^ /%1/%2? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-s 
RewriteCond %{REQUEST_FILENAME} !-l 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !\.(js|ico|gif|jpg|png|css|html|swf)$
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?cat=$1&prod=$2 [B,QSA,L]

Upvotes: 1

Related Questions