Question

I have created 301 redirection code for my website. Following is my code to all my categories to redirect to new url

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^testsite.com/categories/city/$1 [nc]
rewriterule ^(.*)$ http://www.testsite.com/$1 [r=301,nc]

I want to redirect http://www.testsite.com/categories/city/pune to http://www.testsite.com/Pune

Let me know my code is correct or not?

Thanks

Was it helpful?

Solution

It's not correct. The %{HTTP_HOST} is only the host that's provided in the Host: header of the request. It's not going to include any path info. You could separate that condition into 2 things:

RewriteCond %{HTTP_HOST} ^testsite.com$ [NC]
RewriteRule ^/?categories/city/(.*)$ /$1 [R=301,L,NC]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top