Forum Moderators: phranque
Fairly new to all this, so go easy on me! :-)
I've got a dynamic site which has urls as follows:
/cart.php?target=product&product_id=123&category=456
I've succesfully used mod rewrite to rewrite this url e.g.
RewriteRule ^(.*)-(.*)--(.*)--(.*).html /cart.php?target=product&product_id=$4&category=$2 with the other variables being the product names etc.
which works fine, however prior to that I was playing around with few variations like:
RewriteRule ^category(.*)--product(.*).html / cart.php?target=product&product_id=$2&category=$4
Now, it worked so well, that some spiders (msn & google) indexed my original rewritten url, which i've now changed the rules on to those as per the first example.
I've left the old rules in my .htaccess files so that I don't get any 404 errors, and the old rewritten url(s) aren't referenced in my site anymore (but they still work because the rule is still in .htaccess and they are cached in google).
My site is pretty new, and doesnt' have any PR yet, so i'm not worried about that.
I am worried about being penalised for duplicate content though! So, rather than just leaving the rules in there, how do I redirect the old rules to the new one using a 301 redirect? Is this possible? I'm pretty sure I'll need a [R=301,L] or something somewhere, but don't know where or how (and have heard horror stories about loops etc)
Or, should I just remove the old rules and let the pages 404 error and eventully drop out of the google & msn indexes, and let the new ones get indexed?
Any advice appreciated
Cheers
Sebastiaan Stoffels
Welcome to WebmasterWorld!
I can't say exactly, since there are back-references missing in your examples, but all you need to do a redirect something like this (again, the back-references are most likely wrong):
RewriteRule ^category([^-]+)--product([^.]+)\.html$ http://example.com/$1-$2.html [R=301,L]
Also, you are using a very ambiguous and inefficient pattern in your newer rules. Your server will run a lot faster if you use more specific patterns, and use the [L] flag unless you have a specific reason not to:
RewriteRule ^([^-]+)-([^-]+)--([^-]+)--([^-]+)\.html$ /cart.php?target=product&product_id=$4&category=$2 [L]
Jim
the following rules are no longer required:
RewriteRule ^(.*)--(.*)category(.*).html /cart.php?target=product&product_id=$2&category=$3
RewriteRule ^(.*)--c(.*)p(.*).html /cart.php?target=product&product_id=$3&category=$2
RewriteRule ^product(.*).html /cart.php?target=product&product_id=$1
so example of one url i need to redirect is
RewriteRule ^(.*)--c(.*)p(.*).html /cart.php?target=product&product_id=$3&category=$2
needs to actually go to:
RewriteRule ^(.*)-(.*)--(.*)--(.*).html /cart.php?target=product&product_id=$4&category=$2
fyi, the final url is like this:
[mysite.com...]
in reality only the id no's for category and product are needed to parse the url in my script.
in the above example, backreference $2 and $3 of old url need to redirect to backreference $2 and $4 respectively in the new url.
unfortunately, even though i only had the first rule(url) on for a day, it got indexed, and now i've changed the rule to make the new url format. So in terms of SEO i have duplicate content with different urls
I'm thinking of just deleting all the rules i don't want from the .htaccess file and letting those page 404 and drop out of the SE caches, better than getting penalised I suppose.
I'm kind of understanding your example, but am a little confused as to how to apply it to my specific case.
Any further clarification would be great! (my brain hurts a bit though!)
thanks for the assistance so far! :-)
cheers
Seb
RewriteEngine on
# Internally [b]rewrite[/b] static URL requests to cart.php
RewriteRule ^([^-]+)-([^-]+)--([^-]+)--([^.]+)\.html$ /cart.php?target=product&product_id=$4&category=$2 [L]
RewriteRule ^([^-]+)-([^.]+)\.html$ /cart.php?target=category&category_id=$2 [L]
RewriteRule ^([^-]+)--([^.]+)\.html$ /cart.php?target=product&product_id=$2 [L]
RewriteRule ^cart\.html$ /cart.php [L]
#
# The following rules [b]redirect[/b] old-format static URLs found in search engines to the
# new-format static URLs that will be rewritten to the script by the rules above. The dummy
# parameter "null" is used where necessary. Change it to whatever you like -- including blank
# if you want.
#
RewriteRule ^([^-]+)--(.+)category([^.]+)\.html$ http://example.com/null-$3--null--$2.html [R=301,L]
RewriteRule ^([^-]+)--c(.+)p([^.]+)\.html$ http://example.com/null-$2-null-$3.html [R=301,L]
RewriteRule ^product([^.]+)\.html$ http://example.com/null--$1.html [R=301,L]
#
# If "cart.php" is directly requested by a client, redirect it to the static URL so that
# the dynamic URLs won't be indexed by search engines.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /cart\.php?target=product&product_id=([^&]+)&category=([^&]+)
RewriteRule ^cart\.php$ http://example.com/null/%2/null/%4.html [R=301,L]