Forum Moderators: phranque
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
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...]
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]
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]
Jim