mod_rewrite old URL to new URL


/ Published in: Apache
Save to your folder(s)

Great little bit of code for if you are rewriting URLs but then want the old GET URLs to rewrite to their rewritten URLs (phew). Good for telling Google where the new pages are with a 301 rather than getting both indexed.

In this example, i had a page for casual menswear which was 'www.domain.com/product_list.php?cat=3' but i rewrote this URL to be 'www.domain.com/casual' only problem is that because they both still existed Google still had the '?cat=3' page indexed as the casual page, so this bit of code 301 redirects anyone wanting the '?cat=3' to the new '/casual' page.


Copy this code and paste it in your HTML
  1. RewriteRule ^casual?$ product_list.php?cat=3 [L]
  2. RewriteCond %{ENV:REDIRECT_STATUS} ^$
  3. RewriteCond %{QUERY_STRING} ^(.*&)?cat=3(&.*)?$ [NC]
  4. RewriteRule ^product_list\.php$ /casual? [NC,R=301,L]

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.