skip to Main Content

I am trying to rewrite all the old oscommerce links to a new website. But I am having trouble with part of the URL I need to rewrite.

The link looks like this:

http://www.domain.com/product_info.php?cPath=3_72&products_id=129&osCsid=6j3iabkldjcmgi3s1344lk1285

This rewrite works for the above link:

RewriteCond %{REQUEST_URI}  ^/product_info.php$
RewriteCond %{QUERY_STRING} ^cPath=3_72&products_id=129&osCsid=([A-Za-z0-9-_]+)$
RewriteRule ^(.*)$ http://www.domain.com/apple/air.html? [R=301,L]

But will not work for:

http://www.domain.com/product_info.php?cPath=3_72&products_id=129

My problem is that I want the rewrite to work no matter if the &osCsid=6j3iabkldjcmgi3s1344lk1285 part is included or not.

2

Answers


  1. I think you can achieve this by not specifying the closing delimiter ($)

    Give this a try:

    RewriteCond %{REQUEST_URI}  ^/product_info.php$
    RewriteCond %{QUERY_STRING} ^cPath=3_72&products_id=129
    RewriteRule ^(.*)$ http://www.domain.com/apple/air.html? [R=301,L]
    

    By not putting the $ at the end of the regex string you are basically saying: match any string that starts with …, no matter what comes after

    Hope this helps 🙂

    Login or Signup to reply.
  2. This should do the job just fine:

    RewriteCond %{QUERY_STRING} ^cPath=3_72&products_id=129
    RewriteRule ^product_info.php$ http://www.domain.com/apple/air.html? [R=301,L]
    
    1. There is no need for separate condition RewriteCond %{REQUEST_URI} ^/product_info.php$ — this part can be (actually, SHOULD BE, for better performance) moved to RewriteRule.

    2. This is enough ^cPath=3_72&products_id=129 — it tells “When query strings STARTS with …”. No need to include optional/non-important parameters osCsid=([A-Za-z0-9-_]+).

    3. This rule is to be placed in .htaccess file in website root folder. If placed elsewhere some small tweaking may be required.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search