Forum Moderators: phranque

Message Too Old, No Replies

Dynamic 301 divert

         

David3733

8:33 am on Apr 21, 2009 (gmt 0)

10+ Year Member



I have a site that i have updated and before i open it i am trying to keep all the old links but they are presented differently in the search engines

old link
/test/product_info.php?pName=bh-ea-14-srl-metabo-battery-replacement

new link
/test/bh-ea-14-srl-metabo-battery-replacement-p-2826.html

here is my HTaccess rules
RewriteCond %{QUERY_STRING} ^options\=(.*)$
RewriteRule ^(.*)-p-(.*).html$ product_info.php?products_id=$2%1
RewriteRule ^(.*)-p-(.*).html$ product_info.php?products_id=$2&%{QUERY_STRING}
RewriteRule ^(.*)-c-(.*).html$ index.php?cPath=$2&%{QUERY_STRING}
#RewriteRule ^product_info\.php?pName=(.*).html$ =$2&%{QUERY_STRING}
RewriteRule ^(.*)-m-(.*).html$ index.php?manufacturers_id=$2&%{QUERY_STRING}
RewriteRule ^(.*)-pi-(.*).html$ popup_image.php?pID=$2&%{QUERY_STRING}
RewriteRule ^(.*)-t-(.*).html$ articles.php?tPath=$2&%{QUERY_STRING}
RewriteRule ^(.*)-a-(.*).html$ article_info.php?articles_id=$2&%{QUERY_STRING}
RewriteRule ^(.*)-pr-(.*).html$ product_reviews.php?products_id=$2&%{QUERY_STRING}
RewriteRule ^(.*)-pri-(.*).html$ product_reviews_info.php?products_id=$2&%{QUERY_STRING}
RewriteRule ^(.*)-pm-([0-9]+).html$ info_pages.php?pages_id=$2&%{QUERY_STRING}
RewriteRule ^(.*)-i-(.*).html$ information.php?info_id=$2&%{QUERY_STRING}
RewriteRule ^(.*)-links-(.*).html$ links.php?lPath=$2&%{QUERY_STRING}

rewriteCond %{QUERY_STRING} ^[p]Name=
rewriteRule ^product_info\.php\?[p]Name=$ /test/$2 [R=301,L]

This last piece is my work but i do not seem to be getting the right end of the stick

Could somebody please point me in the right direction, i have been googleing all night and not found an answer

Thank you

g1smd

8:39 pm on Apr 21, 2009 (gmt 0)

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



Every rule needs to have [L] added to the end otherwise you'll end up with odd effects.

Never use (.*) at the beginning or in the middle of a rule, as it is very inefficient. The .* matches the whole URL and then has to back-off-and-retry multiple times before a match is found.

Use a more specific pattern, such as

([^/]+)/
to match up to the next slash, or similar.

Every backreference you create must be passed to your script and verified as containing the correct value for that request, otherwise you can end up with problems.

This recent thread explains that in a lot more detail: [webmasterworld.com...]

jdMorgan

3:38 am on Apr 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm afraid that using mod_rewrite, the only way to do this is to use one rule per product, such as

RewriteCond %{QUERY_STRING} ^pName=bh-ea-14-srl-metabo-battery-replacement$
RewriteRule ^product_info\.php$ http://www.example.com/test/bh-ea-14-srl-metabo-battery-replacement-p-2826.html? [R=301,L]

(I presume that this code goes into /test/.htaccess, otherwise "test/" needs to be added to the beginning of the RewriteRule pattern.)

The problem is that mod_rewrite has no way to "know" the association of "bh-ea-14-srl-metabo-battery-replacement" with "p-2826", and so cannot "create" the "product number" part of the new URL out of thin air.

An alternative approach, and one that also addresses the concern that g1smd raises, is to simply rewrite all old-style URLs to a script (possibly your existing script) where you can detect the old URL format, validate the "keywords" part of that old URL, look up the new URL in your database, and output both a "Location:" header and a "Status:" header to generate an external redirect to the new URL. More info on this can likely be found by searching our PHP forum.

If the "keywords" part of the old URL isn't valid, then return a 404-Not Found. If the product is no longer available and no direct replacement product exists, then return a 410-Gone.

If your existing script is "custom" you may not want to modify it. In that case, you can likely copy a very small part of it into a new script, and modify that to generate the redirects you need.

As an example of how to speed up your pre-existing rules, here's your second rule with modifications:


RewriteRule ^(([^\-]+-)*[^-]+)-p-(([^\-]+-)*[^.]+)\.html$ product_info.php?products_id=$3 [QSA,L]

Note that using [QSA] is equivalent to manually appending the original query string variables, except that the order may differ; The main performance saving here is in the regex patterns which can be parsed much more quickly than your multiple-".*" patterns.

Jim