htaccess重定向到子域

时间:2019-05-28 05:45:07

标签: apache .htaccess

当前,我有一个网站(example.com)。 http到https重定向通过我的.htaccess文件起作用

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

此网站还可以在子域上运行。

如果有人访问该网站的www.subdomain.example.com,我希望将他们重定向到https://subdomain.example.com

是否可以使用htaccess文件实现此目标而不会影响example.com所使用的http到https重定向?

谢谢!

1 个答案:

答案 0 :(得分:3)

是的,您可以添加多个条件。

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^www\.subdomain\.example\.com$ [NC]
RewriteRule ^(.*)$ https://subdomain.example.com/$1 [R=301,L]

RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

1:如果主机为www.subdomain.example.com,则重定向到https://subdomain.example.com/

2:如果不是https,请重定向到https

相关问题