Redirect 指令不会绑定到RewriteCond 条件,并且总是将 /my-page 重定向到 http://www.newurl.com/your- page,此外,mod_rewrite 优先于 mod_alias,因此 RewriteRule (.*)$ http://www.newurl.com/$1 [R=301,L] 规则在查看 Redirect 指令之前应用。但是,如果 .htaccess 文件位于 oldurl.com 和 newurl.com 域的文档根目录中,则在浏览器获取后将应用 Redirect 指令重定向到http://www.newurl.com/my-page,从而(再次)重定向到http://www.newurl.com/your-page
因此,您将这些按什么顺序排列并不重要,因为首先应用了 mod_rewrite。如果您有 20 个 URL 需要重定向到新站点上的新 URL,则可以在各自的 Redirect 中枚举它们。否则,如果您不想让浏览器被重定向两次,您可以使用 mod_rewrite 引擎枚举它们:
RewriteEngine On
# redirect the changed URLs individually
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule ^my-page$ http://www.newurl.com/your-page [R=301,L]
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule ^my-page2$ http://www.newurl.com/your-page2 [R=301,L]
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule ^my-page3$ http://www.newurl.com/your-page3 [R=301,L]
# Finally, redirect everything else as-is
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule (.*)$ http://www.newurl.com/$1 [R=301,L]
请注意,这里的顺序确实很重要。必须重复 HTTP_HOST 的 2 个条件有点难看,你可以通过使用 SKIP 来解决这个问题,但最好只是重复它们。但是,如果您有权访问您的服务器配置或虚拟主机配置,请查看RewriteMap Directive,它允许您在您的情况下创建旧 url 到新 url 的映射,并且您可以减少所有单独更改的 url 重写一个人:
在您的服务器/虚拟主机配置中,如下所示:
RewriteMap newurls txt:/path/to/file/map.txt
/path/to/file/map.txt 看起来像这样:
my-page your-page
my-page2 your-page2
my-page3 your-page3
etc...
您的组合规则如下所示:
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule ^(.*)$ http://www.newurl.com/${newurls:$1} [R=301,L]
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule (.*)$ http://www.newurl.com/$1 [R=301,L]