0

我需要更改链接到我网站的所有 URL 中的一个词。我尝试了以下规则,但只有当单词是 URL 中的第一个单词时它才能正常工作

RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteCond %{REQUEST_URI} ^/foo(?:/)?(.*)$
RewriteRule ^[/]?foo[/]?(.*) /bar/$1 [R=permanent,L,NE]

当我向 REQ_URI 添加通配符条目时,输出不正确,我不确定如何处理替换。www.example.com/abc/def/foo/ghi 被重写为 www.example.com/bar/abc/def

RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteCond %{REQUEST_URI} ^(.*)/foo(?:/)?(.*)$
RewriteRule ^(.*)[/]?foo[/]?(.*) /bar/$1 [R=permanent,L,NE]

如何更正重写规则?

4

1 回答 1

2
RewriteRule ^(.*)/foo/(.*)$ $1/bar/$2 [R=301,L]
RewriteRule ^foo/(.*)$ bar/$1 [R=301,L]

This assumes foo is a folder name. Generally when setting something like this up you want to have some sort of delimiter (in this case the slashes) so that, for example, "food" doesn't become "bard".

于 2012-12-05T13:11:54.820 回答