【问题标题】:.htaccess force www. and https for specific domain.htaccess 强制 www。和特定域的 https
【发布时间】:2016-06-15 10:48:40
【问题描述】:

我有一个由多个域访问的服务器。 所有这些域都必须有 www。在前面。

只有一个域应该是 HTTPS,另一个是 HTTP,因为我只有一个域的 SSL 证书。

我在 .htaccess 文件中尝试了以下代码

重写引擎开启

# First force domain to use www.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Then force https
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^amanvida.eu [NC,OR]
RewriteCond %{HTTP_HOST} ^www.amanvida.eu [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NC]

应该强制 https 的域是 www.amanvida.eu

但是它没有按预期完全工作 https://www.amanvida.eu/nl/amanprana-serum-gezicht-shangri-la.html - 工作,当然

http://www.amanvida.eu/nl/amanprana-serum-gezicht-shangri-la.html - 有效

http://amanvida.eu/nl/amanprana-serum-gezicht-shangri-la.html - 有效

https://amanvida.eu/nl/amanprana-serum-gezicht-shangri-la.html - 重定向到主页

所以 4 种情况中有 3 种已经正常工作,但我想要用户已经在 https 站点上但不在 www 上的情况。域也可以正确重定向到页面,而不是主页。

我什至尝试像这样强制 www:

# First force domain to use www.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

没有成功。

【问题讨论】:

    标签: .htaccess https


    【解决方案1】:

    要对特定域强制使用 https 和 www,您可以使用:

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^(www\.)?domain.com$
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://www.domain.com%{REQUEST_URI} [NC,L,R]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-05
      • 2014-01-20
      • 2015-07-18
      • 2013-05-31
      • 1970-01-01
      • 2011-09-14
      • 2015-05-10
      • 2016-11-12
      相关资源
      最近更新 更多