使用.htaccess将http://www.domain.com重定向到https://www.domain.com

时间:2013-10-24 10:11:28

标签: apache .htaccess mod-rewrite redirect

我想将我的域名从http重定向到https:

http://www.domain.com --> https://www.domain.com

有可能吗?我一直在网上搜索,但我只发现:

http://domain.com --> https://www.domain.com

问题是,人们如何直接抵达http://www.domain.com?它们不是非https网址吗?相反,反之亦然。我只想要从HTTP到HTTPS的简单重定向。可能吗?

谢谢

4 个答案:

答案 0 :(得分:2)

I just want a simple redirection from the HTTP to HTTPS

尝试使用此简单规则作为.htaccess 的第一条规则:

RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

答案 1 :(得分:1)

使用以下代码强制使用www和SSL:

RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

答案 2 :(得分:1)

它适用于所有版本的php,并将强制SSL和www到所有裸域和链接:

RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
  • 我们需要NE
  • 万维网。在第4行是可选的
  • “off”必须是。

答案 3 :(得分:1)



<IfModule mod_rewrite.c>

RewriteEngine On
RewriteCond %{HTTPS} off

# Now, rewrite any request to the wrong domain to use www.
RewriteCond %{HTTP_HOST} !^www\.

RewriteCond %{HTTP_HOST} ^tatwerat\.com [NC]

RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}

</IfModule>
&#13;
&#13;
&#13;

相关问题