使用子目录改进 SSL、非 WWW 的 htaccess 重写
Improve htaccess rewrite for SSL, non WWW using subdirectories
所有可能的域名写法都改写为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 让它工作了,但是这段代码有点乱,我很确定有一种 nicer/cleaner 方法可以实现我想要的。
有人可以帮我改进以下 .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/
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ /sub/
# Force https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
主域的URL应该是这样的:https://example.com and sub domain should be like https://sub.example.com
您可以使用这些规则
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]
所有可能的域名写法都改写为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 让它工作了,但是这段代码有点乱,我很确定有一种 nicer/cleaner 方法可以实现我想要的。
有人可以帮我改进以下 .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/
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ /sub/
# Force https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
主域的URL应该是这样的:https://example.com and sub domain should be like https://sub.example.com
您可以使用这些规则
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]