.htaccess redirect to “https://www”

南楼画角 提交于 2019-12-31 04:08:14

问题


I'm looking for an .htaccess configuration that will redirect all requests to my site to https://www.

So whether the user visits:

www.example.com
http://example.com
http://www.example.com
https://example.com

They will always end up at:

https://www.example.com

Here is what I am currently using in my .htaccess file, however it doesn't redirect if a user visits:

www.example.com

Here is my current file contents:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule (.*) https://www.example.com/$1 [R=301,L]

I've tried various solutions available on Stack Overflow, but none of them seem to work as intended, often leading to redirect loops. Hopefully someone will have a better understanding of this than I do.


回答1:


Your regular expression ^example\.com doesn't match www.example.com. You probably want something like (^|\.)example\.com or (^|\.)example\.com($|:).

Edit:

To prevent the redirect loop add

RewriteCond %{HTTPS} off

Before the RewriteRule.

Also see http://wiki.apache.org/httpd/RewriteHTTPToHTTPS (they use !=on instead of off) and http://wiki.apache.org/httpd/RedirectSSL



来源:https://stackoverflow.com/questions/14237805/htaccess-redirect-to-https-www

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