我一直在努力管理从www.domain.com到domain.com的重定向, 但似乎没有什么对我有用。 我总是得到一个重定向循环 - 我尝试过在这里或Google上发现的各种内容。
所以这是我的.htaccess,也许有人可以帮我弄清楚我可以做些什么来正确重定向或者这里有什么问题。
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
# Redirect all to .php
# Example: example.com/hello -> example.com/hello.php
RewriteRule ^(.*)$ $1.php [L,R=301]
# show example.com/index.php always as example.com/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://example.com/ [R=301,L]
非常感谢你! 我已经花了很多时间试图解决这个问题。
答案 0 :(得分:1)
您有一个始终匹配的规则,它负责无限重定向。我已在下面更新了您的规则集以解决该问题并执行您在答案顶部提到的重定向。如果这符合您的期望,请告诉我。
RewriteEngine On
# Redirect www.example.com to example.com
RewriteCond %{HTTP_HOST} ^www [NC]
RewriteRule ^.*$ http://example.com/$0 [R=301,L]
# This performs an external redirection? Is that what you want?
# Don't do the rewrite if we're already pointing at a file, otherwise we'll
# just redirect over and over because .* matches what we redirect to, too
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.php$
RewriteRule ^.+$ $0.php [L,R=301]
# show example.com/index.php always as example.com/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://example.com/ [R=301,L]
答案 1 :(得分:0)
答案是Apache documentation,文档说明如何强制使用www。你只需要反转这个例子。
RewriteCond %{HTTP_HOST} !^example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://example.com/$1 [L,R]