htaccess redirect to “https://www.”

丶灬走出姿态 提交于 2019-12-12 21:13:16

问题


I would like to make a redirection to https://www.

Which means that:

http:// --> https://www.
http://www. --> https://www.
https:// --> https://www.
https://www. --> no redirection

I tried plenty of things but never reached that goal.

my current .htacces:

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{HTTP:X-Forwarded-Proto} =http
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ web/$1 [QSA,L]
</IfModule>

But the problem is that it doesn't redirect to www. So I tried to put that before :

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ www.%{HTTP_HOST}/$1 [R=301,L]

But that made a redirection curl so shut my site down.

PS: I'm on symfony

Does someone have the magic solution ? Thank's !


回答1:


You can use these rules to enforce both https andwww`:

RewriteCond %{HTTP_HOST} !^www\. [OR,NC]
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,R=301,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ web/$1 [L]


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

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