Mobile app version of vmapp.org
Login or Join
Rivera981

: Htaccess 301 redirect for second level sub directory I have a directory website with the following URL structure. http://mywebsite.com/information/business-name/ http://mywebsite.com/information/business-name/product-names/

@Rivera981

Posted in: #301Redirect #Htaccess #ModRewrite

I have a directory website with the following URL structure.
mywebsite.com/information/business-name/ http://mywebsite.com/information/business-name/product-names/ 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 -
mywebsite.com/information/new-business-name/ http://mywebsite.com/information/new-business-name/product-names/ 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/ [R=301,L]
RewriteRule ^reviews/business-name/(.*)$ /reviews/new-business-name/ [R=301,L]
RewriteRule ^comments/business-name/(.*)$ /comments/new-business-name/ [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/ ?

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Rivera981

1 Comments

Sorted by latest first Latest Oldest Best

 

@Ann8826881

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/(.*)$ //new-business-name/ [R=301,L]


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

Depending on your situation you could make this more generic:

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


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

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme