【发布时间】:2018-01-31 07:53:13
【问题描述】:
所有可能的域名写法都应该改成https和非www。即
example.com => https://example.com
www.example.com => https://example.com
http://example.com => https://example.com
http://www.example.com => https://example.com
https://www.example.com => https://example.com
这些规则也适用于所有子域(https 和非 www)。
主站点应该被重写到一个名为“main”的文件夹中。目前我有一个子域,应该重写到一个名为“sub”的文件夹中。
所以我通过 .htaccess 让它工作了,但是这段代码有点乱,我很确定有一个更好/更干净的方法来实现我想要的。
有人可以帮我改进以下 .htaccess 吗?
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.
RewriteCond http%1://%{HTTP_HOST} ^(https?://)(www\.)?(.+)$
RewriteRule ^ %1%3%{REQUEST_URI} [R,L]
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/main/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /sub/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ /sub/$1
# Force https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
主域的 URL 应为:https://example.com,子域的 URL 应为 https://sub.example.com
【问题讨论】:
标签: .htaccess https url-rewriting subdirectory