Laravel 仅在非www上强制使用HTTPS
现行代码Laravel 仅在非www上强制使用HTTPS,laravel,apache,.htaccess,url-rewriting,Laravel,Apache,.htaccess,Url Rewriting,现行代码 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 Traffic重定向到HT
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 Traffic重定向到HTTPS www,但需要离开直接访问的www Traffic而不强制使用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安装。假设您有两个虚拟主机,我将使用您的重定向规则:
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://ANYTHING
-->$1
because of `RewriteCond %{HTTP_HOST} ^test\.com$`
由于RewriteCond%{HTTPS}关闭
。为什么?由于[或]
选项。它应该是一个“[和]”,这是隐式的。因此,请删除[或]
。第二个也一样
在第二组指令中,你也在自相矛盾。它说:
- 它是www.test.com
- 或test.com
- 不是目录
- 不是档案
- 转到index.php
您说过要将非www请求重定向到https 因此,您需要:
- -->停留在
- -->重定向到
- -->in
- -->in
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>
听80
服务器名www.test.com
ServerAlias test.com
[…其他配置…]
重新启动发动机
# http://test.com -->重定向到
#其他的都留在里面
重写cond%{HTTP_HOST}^test\.com$
重写规则^(.*)$https://test.com/$1[R=301,L]
#默认页面index.php,避免404
重写cond%{HTTP_HOST}^www.test.com$
重写cond%{REQUEST_FILENAME}-D
重写cond%{REQUEST_FILENAME}-F
重写规则^index.php[L]