Forum Moderators: phranque

Message Too Old, No Replies

Duplicate URL problem

duplicate url

         

thatsme

10:33 am on Jul 2, 2009 (gmt 0)

10+ Year Member



I have a site which uses mod_rewrite for rewriting pages to SEO freindly url's, like:

http://www.example.com/details.php?id=myid
to
http://www.example.com/myid-page.html

now the problem is somehow the first page with query string got indexed in google and I want to now do 301 permanent redirect on above page to .html page.

But when I try to use, I get following problems:

- "Redirect 301" does not works because it has a query string
- RewriteRule with RewriteCond takes it in a loop, given below the rule I am using:

RewriteCond %{QUERY_STRING} ^id=myid$
RewriteRule ^/details\.php http://www.example.com/myid-page.html? [R=301,L]

Can any one suggest how to avoid user typing urls with querystring, which should auto redirect to the HTML version?

jdMorgan

3:01 pm on Jul 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One of our most-frequently-asked questions here... :)

Assuming that this code goes into a server config file, and is not within a <Directory> section:


RewriteCond %{THE_REQUEST} ^[A-Z]+\ /details\.php\?id=myid\ HTTP/
RewriteRule ^/details\.php$ http://www.example.com/myid-page.html? [R=301,L]

Testing THE_REQUEST (which contains the client HTTP request line exactly as it appears in your raw server access log) guarantees that the rule is invoked only if the "unfriendly" path is being requested as a URL by the client, and not as an internal path request resulting from your "friendly-URL-to-script-filepath" internal rewrite. So this rule will only redirect direct client requests for the old unfriendly URL, and won't conflict with your internal rewrite and create a loop.

Jim