当前代码
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.test.com$ [OR]
RewriteCond %{HTTP_HOST} ^test.com$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
我需要将非www流量重定向到HTTPS www,但需要保留直接访问的www流量而不执行HTTPS。
所以我尝试了
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^test\.com$ [OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://test.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.test.com$ [OR]
RewriteCond %{HTTP_HOST} ^test.com$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
但是我收到的“重定向太多”,不知道为什么。 index.php来自laravel安装。
答案 0 :(得分:1)
假设您有2个VirtualHost,我正在使用您的重定向规则:
Listen 80
<VirtualHost *:80>
ServerName www.test.com
ServerAlias test.com
[... OTHER CONFIGURATION ...]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^test\.com$ [OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://test.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.test.com$ [OR]
RewriteCond %{HTTP_HOST} ^test.com$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</VirtualHost
Listen 443
<VirtualHost *:443>
ServerName www.test.com
ServerAlias test.com
[... OTHER CONFIGURATION ...]
</VirtualHost>
您需要http://test.com
-> this $ 1
because of `RewriteCond %{HTTP_HOST} ^test\.com$`
您需要http://ANYTHING
-> https://test.com/ $ 1
因为RewriteCond %{HTTPS} off
。为什么?由于有[OR]
选项。它应该是一个'[AND]',它是隐式的。因此,删除[OR]
。第二件事也是一样。
您在第二组指令中也与自己矛盾。它说:
您说过要将非www请求重定向到https。
因此,您想要:
<VirtualHost *:80>
<VirtualHost *:443>
<VirtualHost *:443>
<VirtualHost *:443>
此配置将执行以下操作:
Listen 80
<VirtualHost *:80>
ServerName www.test.com
ServerAlias test.com
[... OTHER CONFIGURATION ...]
RewriteEngine On
# http://test.com --> redirect to <VirtualHost *:443>
# Everything else stays in <VirtualHost *:80>
RewriteCond %{HTTP_HOST} ^test\.com$
RewriteRule ^(.*)$ https://test.com/$1 [R=301,L]
# Default page index.php, avoid 404
RewriteCond %{HTTP_HOST} ^www.test.com$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</VirtualHost
Listen 443
<VirtualHost *:443>
ServerName www.test.com
ServerAlias test.com
[... OTHER CONFIGURATION ...]
# Default page index.php, avoid 404
RewriteCond %{HTTP_HOST} ^www.test.com$ [OR]
RewriteCond %{HTTP_HOST} ^test.com$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</VirtualHost>