2

I have a directory website with the following URL structure.

http://mywebsite.com/information/business-name/
http://mywebsite.com/information/business-name/product-names/    
http://mywebsite.com/reviews/business-name/
http://mywebsite.com/comments/business-name/

Now, there is a change in the business name and I want to change the URLs to these -

http://mywebsite.com/information/new-business-name/
http://mywebsite.com/information/new-business-name/product-names/    
http://mywebsite.com/reviews/new-business-name/
http://mywebsite.com/comments/new-business-name/

How do I 301 redirect the old URLsto new URLs in htaccess file?

Currently, I am temporarily using the following -

RewriteRule ^information/business-name/(.*)$ /information/new-business-name/$1 [R=301,L]
RewriteRule ^reviews/business-name/(.*)$ /reviews/new-business-name/$1 [R=301,L]
RewriteRule ^comments/business-name/(.*)$ /comments/new-business-name/$1 [R=301,L]

Is there a way to rewrite it in one rule as I just need to change /business-name/ to /new-business-name/ ?

1 Answer 1

2

To combine those three rules into one you can use alternation (with a vertical bar) in a capturing group and another backreference in the substitution:

For example:

RewriteRule ^(information|reviews|comments)/business-name/(.*)$ /$1/new-business-name/$2 [R=301,L]

$1 is now a backreference to either "information", "reviews" or "comments".

Depending on your situation you could make this more generic:

RewriteRule ^([^/]+)/business-name/(.*)$ /$1/new-business-name/$2 [R=301,L]

This will match any single subdirectory that precedes "/business-name/", including "information", "reviews" and "comments".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.