.htaccess force www。和特定域的https [英] .htaccess force www. and https for specific domain

查看:146
本文介绍了.htaccess force www。和特定域的https的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a server which is accessed by several domains. All of those domains MUST have www. in front.

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

Only 1 domain should be HTTPS, the other's HTTP since I only had an SSL certificate for on domain.

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

I tried the following code in my .htaccess file

RewriteEngine On

RewriteEngine On

# 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

The domain that should force https is www.amanvida.eu

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

However it is not working fully as expected https://www.amanvida.eu/nl/amanprana-serum-gezicht-shangri-la.html - works, of course

http://www.amanvida.eu/nl/amanprana-serum-gezicht-shangri-la.html - 作品

< a href =http://amanvida.eu/nl/amanprana-serum-gezicht-shangri-la.html =nofollow> http://amanvida.eu/nl/amanprana-serum-gezicht-shangri- la.html - 作品

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

https://amanvida.eu/nl/amanprana-serum-gezicht-shangri-la.html - redirects to the home page

所以3出4种情况已经正常工作,但我希望用户已经在https网站但不在www上的情况。域也可以正确地重定向到页面,而不是主页。

So 3 out of 4 situations are already working fine, but I want the case where the user is already on the https site but not on the www. domain to also redirect correctly to the page, and not to the home page.

我甚至尝试强制这样的www:

I even tried forcing www like this:

# 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]

没有成功。

推荐答案

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

To force https and www for a specific domain, you can use :

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

这篇关于.htaccess force www。和特定域的https的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆