问题原文
原帖链接 · Aakash · CC BY-SA 3.0
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/ ?
已采纳解答
原帖链接 · MrWhite · CC BY-SA 3.0
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".
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
