Forum Moderators: phranque

Message Too Old, No Replies

Help with consecutive mod rewrite rules

Old URL-> friendly URL -> PHP URL

         

redox

9:44 am on Feb 3, 2011 (gmt 0)

10+ Year Member



Hello,

I have a PERL script running I coded years ago where I used a blank in an URL which resulted in a bunch of permutated URLs over the years, e.g.

/cgi-bin/page.pl?page=product_overview
/cgi-bin/page.pl?page=product overview
/cgi-bin/page.pl?page=product%20overview
/cgi-bin/page.pl?page=product%2520overview

After a site relaunch the same content is now delivered by:
/products.php?page=overview

and should be reached user friendly:
/products

So I have to do 2 consecutive rewrites. Reading around in this forum and other boards I ended up with this solution:

RewriteCond %{QUERY_STRING} ^page=product(.+)$ [NC]
RewriteRule ^cgi-bin/page\.pl$ /products [R=301,L]
RewriteRule ^products$ /products.php?page=overview [L]


This seems to work. I'm just starting to enter the world of mod_rewrite. I'm aware of the dangers of improper usage of the module so any comments would be greatly appreciated.

Thanks

g1smd

11:20 pm on Feb 3, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The 301 redirect needs to include the protocol and the domain name, otherwise non-www requests will redirect to non-www, and www requests will redirect to www.

Omitting the domain name promotes duplicate content.

Add a question mark to the end of the 301 redirect target, otherwise the redirect will re-append the original query string data in full.

redox

2:38 am on Feb 4, 2011 (gmt 0)

10+ Year Member



Thank you very much for your help. I added the question mark.

RewriteCond %{QUERY_STRING} ^page=product(.+)$ [NC]
RewriteRule ^cgi-bin/page\.pl$ [domain.com...] [R=301,L]
RewriteRule ^products$ /products.php?page=overview [L]


Regarding the duplicate content: Could I omit protocol and domain name by using the general non-www to www redirect at the end of my rewrite rules?

RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ [domain.com$1...] [L,R=301]


Thanks again
redox

g1smd

2:44 am on Feb 4, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



NO. In that case you would get an unwanted multi-step redirection chain for some requests.

By stating the domain name in every redirect, only one redirect is needed for any "wrong" URL request.


The non-www to www redirect should be listed as the last redirect, and MUST be listed BEFORE any of the rewrites.

Failure to do so, risks exposing previously internally rewritten filepaths back out on to the web as URLs.


Use example.com to stop the forum auto-linking the example URLs.

redox

3:22 pm on Feb 4, 2011 (gmt 0)

10+ Year Member



Thank you very much for your help and for the precise explanations. Hope I can give something back to the forum one day..