如何强制HTTPS到子域(没有www)

时间:2016-06-29 10:19:53

标签: regex .htaccess

强制HTTPS时如何强制任何用户使用以下网址?

https://subdomain.hostname.com/request-uri

来自以下所有链接:

http://hostname.com/request-uri

http://www.hostname.com/request-uri

https://hostname.com/request-uri

https://www.hostname.com/request-uri

我试过了:

RewriteEngine On
RewriteRule .* https://subdomain.hostname.com%{REQUEST_URI} [L,R=301]

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?hostname\.com$
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://subdomain.hostname.com%{REQUEST_URI} [L,R=301]

没有任何成功......

3 个答案:

答案 0 :(得分:2)

您可以使用:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(?:www\.)?hostname\.com$
RewriteRule ^(.*)$ https://subdomain.hostname.com%{REQUEST_URI} [L,R=301]

在测试之前清除浏览器缓存。

答案 1 :(得分:1)

我想,那就是:

RewriteEngine On
RewriteCond %{HTTP_HOST} '=hostname.com' [OR]
RewriteCond %{HTTP_HOST} '=www.hostname.com'
RewriteRule ^(.*)$ https://subdomain.hostname.com/$1 [L,R=301]

这会将请求的所有重定向到 hostname.com www.hostname.com 到子域。

如果您只需要重定向特定的 request-uri ,那么您需要这样的内容:

RewriteCond %{HTTP_HOST} '=hostname.com' [OR]
RewriteCond %{HTTP_HOST} '=www.hostname.com'
RewriteRule ^(.*request-uri)$ https://subdomain.hostname.com/$1 [L,R=301]

答案 2 :(得分:1)

试试这个:

<VirtualHost *:80>
    ServerName www.hostname.com
    Redirect / https://subdomain.hostname.com/
</VirtualHost>

<VirtualHost *:443>
    ServerName www.hostname.com
    # ... SSL configuration goes here
</VirtualHost>

请检查:Simple Redirection