Redirect to HTTP non-www to HTTPS www htaccess

前端 未结 12 1070
醉话见心
醉话见心 2020-12-01 08:05

I want to redirect from any direction to our site with HTTPS protocol, but some redirects it\'s not working. I want this:

  • http://www.site.co TO
相关标签:
12条回答
  • 2020-12-01 08:50

    For example https://example.com should be going to https://www.example.com

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

    Note - you should change example.com to your own domain.

    0 讨论(0)
  • 2020-12-01 08:52

    In case you are using One.com as your webhost you should use the following code instead:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
    RewriteCond %{HTTPS} !=on
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    0 讨论(0)
  • 2020-12-01 08:56

    I have an asp.net website and the host provider is not supporting IIS rewrite module i have tested all this methods and only this code gives woorank.com confirmation maybe useful for another .net developer :

    RewriteEngine on
    RewriteBase /
    
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule (.*) https://www.example.com%{REQUEST_URI} [L,R=301]
    
    RewriteCond %{HTTPS} !on
    RewriteRule (.*) https://www.example.com%{REQUEST_URI} [L,R=301]
    
    0 讨论(0)
  • 2020-12-01 08:56

    Just put the following in the .htaccess file

     RewriteEngine on
    
     RewriteCond %{SERVER_PORT} 80 
     RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    
    0 讨论(0)
  • 2020-12-01 08:58
    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
    
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301] 
    
    0 讨论(0)
  • 2020-12-01 08:59

    The answer by Prix works. To make it more dynamic lets use SERVER_NAME and REQUEST_URI instead of a static domain name.

    RewriteEngine On
    #we replace domain.com/$1 with %{SERVER_NAME}%{REQUEST_URI}.
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^(.*) https://www.%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
    
    #here we dont use www as non www was already redirected to www.
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*) https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
    
    0 讨论(0)
提交回复
热议问题