Aaron Aaron -4 years ago 68
PHP Question

Setting up specific page redirects as well as entire site redirects in .htaccess

I want to setup a redirect on my site to go to a different domain which I am able to do so with this rewrite rule:

RewriteEngine on
RewriteRule ^(.*)$ https://www.newdomain.com/$1 [R=301,L]


This works great but there are also a few pages that I want to completely change the url as well as redirect to the new domain. Here is an example:

1.) Here is an example url to my old domain. https://www.olddomain.com/product-tag/[tag-name]/. I want this go to https://www.newdomain.com/parts?category=[tag-name]/.

2.) Here is an example url to my old domain. https://www.olddomain.com/product-categories/[category-name]/garbage. I want this go to https://www.newdomain.com/parts?category=[category-name]/.

I was hoping that adding something like this would do the trick but it does not seem to be working:

RewriteEngine on
RewriteRule ^/product-tag/(.*)$ https://www.newdomain.com/parts?category=$1 [R=301,L]
RewriteRule ^(.*)$ https://www.newdomain.com/$1 [R=301,L]


How can I achieve this?

Answer Source

Have it like this:

RewriteEngine on 

# handle both specific URL redirects    
RewriteRule ^/?product-(?:tag|categories)/([\w-]+) https://www.newdomain.com/parts?category=$1 [R=301,L,NC,QSA]

# redirect everything else
RewriteRule ^(.*)$ https://www.newdomain.com/$1 [R=301,L,NE]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download