Redirect www. to non-www and redirect all http to https with .htaccess

橙三吉。 提交于 2021-02-05 07:44:28

问题


Million times asked question, but after reading many of the answers here on SO I still can't figure this out. I need to redirect all requests like below:

  1. http://domain.tld > https://domain.tld
  2. http://www.domain.tld > https://domain.tld
  3. https://www.domain.tld > https://domain.tld
  4. http://sub.domain.tld > https://sub.domain.tld (when .htaccess placed in subdomain folder)

Until now, I was using the code from html5boilerplate, that solved www to non-www

<IfModule mod_rewrite.c>
  RewriteCond %{HTTPS} !=on
  RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
  RewriteRule ^(.*)$ http://%1/ [R=301,L]
</IfModule>

they also have a code for http to https redirect, but after adding this piece of code (above the www redirect), page is loading and after a timeout it shows error/too many redirects

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{HTTPS} !=on
  RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>

I've also tried examples like this SO answer but still the website doesn't work. Only solution for now is to use the first piece of code and replace http with https but that doesn't solve most important redirect (1)

current full .htaccess content:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /

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

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule /?([A-Za-z0-9_-]+)/?$ index.php?id=$1 [QSA,L]

    #RewriteCond %{HTTPS} off [OR] #by uncommenting this, site stops working
    RewriteCond %{HTTP_HOST} ^www
    RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
    RewriteRule ^ https://%1%{REQUEST_URI} [R,L]
</IfModule>

回答1:


Sometimes, depending on some server configuration, it appears that HTTPS variable is always set to off. That would explain the loop, and i've already seen a similar case here on SO.

A simple workaround would be to test SERVER_PORT (if you use default 80 and 443) or you could also test HTTP:X-Forwarded-Proto.

Example (1) to test if it is not https:

RewriteCond %{HTTP:X-Forwarded-Proto} !https

Example (2) to test if it is not https:

RewriteCond %{SERVER_PORT} !403



回答2:


To redirect to https and non-www in a single http request, you can use

RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R]

Clear your browser's cache before testing this rule.



来源:https://stackoverflow.com/questions/39317063/redirect-www-to-non-www-and-redirect-all-http-to-https-with-htaccess

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