这是我当前的.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.com至https://example.com
https://www.example.com至https://example.com
http://example.com/file.php至https://example.com/index.php
除了www部分,一切似乎都在工作..请帮忙吗?
答案 0 :(得分:2)
http://www.example.com
至https://example.com
https://www.example.com
至https://example.com
http://example.com/file.php
至https://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} off
或on
RewriteCond %{HTTP:X-Forwarded-SSL} off
或on
答案 1 :(得分:1)
您可以添加一个处理www部分的附加规则
RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteRule ^ https://%1%{REQUEST_URI} [L,R]
RewriteCond
会抓取www.
之后的所有内容,并将RewriteRule
中的内容用作%1
。
如果一切正常,您可以将R
更改为R=301
。
301
的情况下从不测试,请参阅此答案 Tips for debugging .htaccess rewrite rules 详情。