htaccess非www到www而http到https和http:// www到https:// www

时间:2014-09-11 20:25:30

标签: .htaccess

基本上,我想拥有这些

我目前的htaccess就是这个。

Options -Indexes
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?url=$1 [QSA,NC,L]

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI}$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}$1

这样工作正常,但是当您手动转到http://domain.com并将其更改为https://domain.com时,它会添加两组index.php。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我认为您的代码存在的问题是您在$1WWW重定向上有HTTPS,这会在末尾添加请求URI路径两次,因为你已经有%{REQUEST_URI}

Options -Indexes +FollowSymLinks

RewriteEngine On
RewriteBase /

RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}:443%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?url=$1 [QSA,NC,L]

如果您首先将HTTP重定向到HTTPS(如果需要),然后检查WWWW是否缺少重定向(如果需要,再次),那么会更容易。< / p>

可能会将HTTPS规则保留为[R=301,L],但如果网址已经正确,我们希望避免处理更多规则。

请注意,规则的顺序非常重要。

相关问题