query string rewrite and redirect

New to mod_rewrite? This is a good place to start.

query string rewrite and redirect

Postby nikki » Sun Aug 30, 2009 3:47 am

Hello,

I am trying to rewrite the query string /product.php?id=1&brand=brand&name=name to /brand/name-id, and then redirect the initial url to the rewritten url to prevent duplicate content.

However, I'm not having much luck - I wonder if someone could help me please?

Thank you,
Nikki.
nikki
 
Posts: 3
Joined: Wed Aug 26, 2009 12:22 pm

Postby richardk » Sun Aug 30, 2009 11:26 am

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# /product.php?brand=brand&name=name&id=1 --> /brand/name-id
# Only match new requests (stop loops).
RewriteCond %{ENV:REDIRECT_STATUS} ^$
# Match the brand.
RewriteCond %{QUERY_STRING} ^(.*&)?brand=([^&]+)(&.*)?$ [NC]
# Match the name.
RewriteCond %2&%{QUERY_STRING} ^([^&]+)&(.*&)?name=([^&]+)(&.*)?$ [NC]
# Match the id.
RewriteCond %1/%3&%{QUERY_STRING} ^([^&]+)&(.*&)?id=([^&]+)(&.*)?$ [NC]
RewriteRule ^product\.php$ /%1-%3? [R=301,L]

# /brand/name-id --> /product.php?brand=brand&name=name&id=1
RewriteRule ^([^/]+)/([^/]+)-([0-9]+)/?$ /product.php?brand=$1&name=$2&id=$3 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby nikki » Sun Aug 30, 2009 12:07 pm

That worked a treat, thank you very much for your help Richard :)
nikki
 
Posts: 3
Joined: Wed Aug 26, 2009 12:22 pm


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 32 guests

cron