在域名上强制使用 www. 时出现 mod_rewrite 重定向循环

在域名上强制使用 www. 时出现 mod_rewrite 重定向循环

我试图仅在生产服务器上强制使用 www。我不想影响暂存服务器的域。这是我当前的规则:

# Force www
RewriteCond %{HTTP_HOST} !^www.mydomain.com$ #production
RewriteCond %{HTTP_HOST} !^mydomain.s2.mycompany.com$ #staging
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301]

基本上,如果主机不是 www.mydomain 并且主机不是 mydomain.s2.... 那么我想重定向到 www.mydomain.com。使用上面的代码,我得到了一个重定向循环。

有任何想法吗?

答案1

将“Last”或 [L] 标志添加到 RewriteRule 中,例如 - [R=301,L]

RewriteCond %{HTTP_HOST} !^www.mydomain.com$ #production
RewriteCond %{HTTP_HOST} !^mydomain.s2.mycompany.com$ #staging
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]

这应该可以停止循环。

RewriteRule您可以在此处查看指令(及其他指令) 的文档:http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewriterule

相关内容