Redirect http://www.domain.com to https://www.domain.com using .htaccess

扶醉桌前 提交于 2020-01-03 05:03:29

问题


I want to redirect my domain from http to https:

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

Is it possible? I've been searching around the internet but I only found:

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

The question is, how about peoples arriving directly on http://www.domain.com? Aren't they be served with non-https url? Same as the vice versa. I just want a simple redirection from the HTTP to HTTPS. Is it possible?

Thank you


回答1:


It will work for ALL versions of php and will force SSL and www to all naked domains and links:

RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
  • We DO need the NE
  • www. is optional in 4th line
  • "off" is required to be.



回答2:


I just want a simple redirection from the HTTP to HTTPS

Try this simple rule as first rule on your .htaccess:

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



回答3:


Use the following code for force www and SSL usage:

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



回答4:


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


来源:https://stackoverflow.com/questions/19563032/redirect-http-www-domain-com-to-https-www-domain-com-using-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!