【发布时间】:2017-10-03 12:51:27
【问题描述】:
我正在尝试使用 .htaccess 进行重定向:
1) 所有 www 到非 www 例如www.example.com/page.php 到 example.com/page.php
2) 所有 http 到 https 例如http://example.com/page.php 到 https://example.com/page.php
3) 域(和子文件夹)到它们的 index.php 页面,例如https://example.com 到 https://example.com/index.php 和 https://example.com/en-gb/ 到 https://example.com/en-gb/index.php
我可以让 1) 和 2) 正常工作:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)(.+) [OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule ^ https://%2%{REQUEST_URI}
但我也在努力包含 3)。我已经尝试过了,但没有运气:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)(.+) [OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule ^ https://%2%{REQUEST_URI}
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php
更新 - 试过了,但没有成功
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)(.+) [OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule ^ https://%2%{REQUEST_URI}
Redirect / https://example.com/index.php
第二次更新 - 尝试了这个,但与我将 index.php 递归添加到 URL 之前相同:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)(.+) [OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule ^ https://%2%{REQUEST_URI}
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) https://example.com/index.php$1
【问题讨论】: