Ahmed Shah Ahmed Shah - 2 months ago 9
PHP Question

How to Redirecting 301 which is already re-written

My previous re-written rules are working fine as below:

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^product-detail/([\w-.]+)/?$ product-detail.php?e=$1 [NC,L]


It is mysite.com/product-detail/laptop

In searches, 5000 product results are already there.
I want to redirect 301 to new folder as

mysite.com/electronics/product-detail/laptop

I don't want google to destroy my results. Please help me.

Answer

I believe it should be something like that:

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^product-detail/([\w-.]+)/?$ /electronics/product-detail/$1 [NC,R=301,L]
RewriteRule ^electronics/product-detail/([\w-.]+)/?$ product-detail.php?e=$1 [NC,L]

Your 1st, 2nd rules are unchanged. Then, you forward all old URLs to new ones, with redirect code 301. And then, process new URLs to your script, as it was with old URLs.

I assume here that your path prefix electronics is a constant one, i.e. it does not depend on product category. If so, you would have to make the redirect in the script, as Apache will not be able to guess the category of the product (for example, if redirecting from /product-detail/hugo-boss to => /parfume/product-detail/hugo-boss).