Forum Moderators: phranque

Message Too Old, No Replies

301 redirect from one rewritten url to another

301 redirect from mod rewrite url to another

         

Sebastiaan

12:34 am on Aug 9, 2005 (gmt 0)

10+ Year Member



Hello all,

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

jdMorgan

1:38 am on Aug 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sebastiaan,

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]

You are redirecting old URLs to new URLs, not old rules to new rules. A URL exists even when there is no 'file' for it to point to; URLs and files are not the same thing in any way.

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]

In case you haven't used patterns like this before, "[^-]+" means, "match one or more characters up to, but not including, the next hyphen." This very-specific pattern lets the regex parser avoid having to try N-F matching attempts, where N is the number of characters in the requested local URL-path and F is the number of non-variable characters (in this case, five hyphens and the five characters in ".html"). Instead, the pattern match can be tested in a single left-to-right pass, and many times faster.

Jim

Sebastiaan

2:28 am on Aug 9, 2005 (gmt 0)

10+ Year Member



Hi Jd,
Not entirely clear yet, but some twinging is happening in my brain!(see how green i am with regex!), but my entire .htaccess file looks like this:
DirectoryIndex index.php
RewriteEngine On
RewriteRule ^(.*)-(.*)--(.*)--(.*).html /cart.php?target=product&product_id=$4&category=$2
RewriteRule ^(.*)--(.*)category(.*).html /cart.php?target=product&product_id=$2&category=$3
RewriteRule ^(.*)--(.*).html /cart.php?target=product&product_id=$2
RewriteRule ^(.*)--c(.*)p(.*).html /cart.php?target=product&product_id=$3&category=$2
RewriteRule ^(.*)-(.*).html /cart.php?target=category&category_id=$2
RewriteRule ^product(.*).html /cart.php?target=product&product_id=$1
RewriteRule ^cart.html /cart.php

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

jdMorgan

1:06 am on Aug 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, here's a final example. If it's not quite right, then experiment with it and try to get it working:

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]

Jim

Sebastiaan

1:29 pm on Aug 14, 2005 (gmt 0)

10+ Year Member



Hi JD,
Finally got around to experimenting! You got me on the right track, and I've actually learnt something! :-)

Thanks!

Cheers

Seb