Click to See Complete Forum and Search --> : 301 redirect mixing with rewrite rule


BoarsHell
08-14-2008, 10:04 AM
here is my htaccess file...

Redirect 301 /old-product-name/pd/category-name/232-202 http://www.domain.com/new-product-name/pd/category-name/878-979

RewriteCond %{REQUEST_URI} !-s
RewriteRule ^(.*)/pd/(.*)/(.*)$ /pc2/prod.php?Screen=PROD&Category_Code=$2&Product_Code=$3 [L]

The rewrite rule simply makes my dynamic urls look static and it works fine. The problem occurs with the 301 redirect. The redirection is redirecting customers from an old product to a new product and it works fine but it appends extra information to the end of the url. Example...

http://www.domain.com/new-product-name/pd/category-name/878-979?Screen=PROD&Category_Code=category-name&Product_Code=232-202

I want the url to look like this...

http://www.domain.com/new-product-name/pd/category-name/878-979

Anyone have any ideas on how to achieve this?

Thanks,
Paul

goofball
08-14-2008, 11:17 AM
Just an idea...

You could try moving the Redirect rule to the bottom, so that it executes after the RewriteRule. You could protect that url from being rewritten with an extra condition:


RewriteCond %{REQUEST_URI} !/232-202/


So your new .htaccess file might look like:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !-s
RewriteCond %{REQUEST_URI} !/232-202/
RewriteRule ^(.*)/pd/(.*)/(.*)$ /pc2/prod.php?Screen=PROD&Category_Code=$2&Product_Code=$3 [L]
</IfModule>

Redirect 301 /old-product-name/pd/category-name/232-202 http://www.domain.com/new-product-name/pd/category-name/878-979

BoarsHell
08-14-2008, 02:10 PM
Thanks for the suggestion.

I gave it go but it did not change the final url. Any other ideas?

goofball
08-16-2008, 12:20 AM
I would just keep trying to filter the url using a regular expression on the condition part:

RewriteCond %{REQUEST_URI} !/old-product-name/pd/category-name/232-202

I know that .htaccess rewrites can be unpredictable and a pain sometimes... you just have to keep trying things. Also, see the Apache documentation on RewriteRule. One other idea might be to actually create the /old-product-name/ folder and put another .htaccess file in there with your 301 redirect. Maybe having the different .htaccess files would help keep the rules from interfering with each other.

Mr. E. Cryptic
08-16-2008, 04:56 AM
Mode rewrite is mostly vodoo, something that works one min wont the next, so it's often difficult to track down the exact problem if something isn't working, without some testing.

At a glance though, I would guess that the use of (.*) immediately after the domain could be a problem and the rewrite rule is catching requests for
/old-product-name/pd/category-name/232-202 (your redirect)
because it actuall does conform to the rewrite rule.

Have you tried adding a faux directory to the rewrite rule so it differs from the redirect:

^fake/(.*)/pd/(.*)/(.*)/$

with your links redirecting to fake/something/pd/something/else/