1

这是我当前的 .htaccess 文件:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php
</IfModule>

我想将所有http请求重定向到https,将所有www请求重定向到非www,并将所有文件请求重定向到index.php

例如:

http://www.example.comhttps://example.com

https://www.example.comhttps://example.com

http://example.com/file.phphttps://example.com/index.php

除了 www 部分,一切似乎都在工作。请帮忙吗?

4

2 回答 2

2

http://www.example.comhttps://example.com
https://www.example.comhttps://example.com
http://example.com/file.phphttps://example.com/index.php

也许这会起作用:

RewriteEngine On

# Remove www from https requests. 
# Next 3 lines must be placed in one .htaccess file at SSL root folder, 
# if different from non-ssl.
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST}  ^www\.(.+) [NC]
RewriteRule ^(.*) https://%1/$1 [R=301,L]

# Redirect all http to https
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} (?:www\.)?(.+)  [NC]
RewriteRule ^(.*) https://%1/$1     [R=301,L]

如果还是不行,换个试试

RewriteCond %{HTTPS} offon

RewriteCond %{HTTP:X-Forwarded-SSL} off或者on

于 2013-04-25T07:23:44.080 回答
1

您可以添加处理 www 部分的附加规则

RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteRule ^ https://%1%{REQUEST_URI} [L,R]

RewriteCond捕获之后的所有内容并在aswww.中使用它。RewriteRule%1

当一切按预期工作时,您可以更改RR=301.

永远不要在启用的情况下进行测试,有关详细信息301,请参阅此答案 提示调试 .htaccess 重写规则

于 2013-04-25T00:57:15.170 回答