Using htaccess to redirect http://www, http:// and https://www to https:

后端 未结 2 1072
旧时难觅i
旧时难觅i 2021-01-27 16:51

On my site I have installed an SSL certificate on the non www version of the domain. I would like to use htaccess to redirect http www, http non www and https www to https:// <

相关标签:
2条回答
  • 2021-01-27 17:48

    You can use: for http or https, with or without www -> https without www

    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
    
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    

    And for the opposite: for http or https, with or without www -> https with www

    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    
    0 讨论(0)
  • 2021-01-27 17:51

    As per your comments here is one single rule to do following redirects in single rule:

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

    This does:

    http://www.domain.com/foobar  => https://www.domain.com/foobar
    http://domain.com/foobar      => https://www.domain.com/foobar
    https://domain.com/foobar     => https://www.domain.com/foobar
    https://www.domain.com/foobar => Unchanged
    
    0 讨论(0)
提交回复
热议问题