编写域名的所有可能方式都应该重写为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]
主域名的网址应为:https://example.com,子域名应为https://sub.example.com
答案 0 :(得分:2)
您可以使用这些规则
RewriteEngine on
#redirect main site to https non www
RewriteCond %{HTTP_HOST} ^www\. [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(mainsite\.com)$
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R]
#rewrite mainsite to /mainsite folder
RewriteCond %{HTTP_HOST} ^(www\.)?mainsite\.com$
RewriteCond %{REQUEST_URI} !^/mainsitefolder
RewriteRule ^ /mainsitefolder%{REQUEST_URI} [L]
#redirect subdomain to https and subfolder
RewriteCond %{HTTP_HOST} ^sub.domain.com$ [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(sub\.domain\.com)
RewriteRule ^ https://sub.domain.com%{REQUEST_URI} [NE,L,R]
#rewrite subdomain to subfolder
RewriteCond %{HTTP_HOST} ^sub.domain.com$
RewriteCond %{REQUEST_URI} !^/subdomainfolder
RewriteRule ^ /subdomainfolder%{REQUEST_URI} [L]