Redirect to HTTP non-www to HTTPS www htaccess

前端 未结 12 1069
醉话见心
醉话见心 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:33

    I use this:

    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [END,NE,R=permanent]
    
    0 讨论(0)
  • 2020-12-01 08:35

    This will use for both www or non-www If you try to open link with www then url redirect to https with www

    Example : http://domain.com redirect to https://domain.com
    

    or If you try to open link with non-www then url redirect to https with non-www

    Example : http://www.domain.com redirect to https://www.domain.com
    
    RewriteEngine on
    
    RewriteCond %{SERVER_PORT} 80 
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    
    0 讨论(0)
  • 2020-12-01 08:45

    This is http to https and non www to www redirection using .htaccess

    If you want to redirect to https and www you can use this code

    http to https redirection:

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

    Non www to www redirection:

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

    If you want to use booth functionality place the above all code respectively

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

    Try this methode:

    # Redirect 301 all to https://www
    <IfModule mod_rewrite.c>
       RewriteCond %{HTTPS} off [OR]
       RewriteCond %{HTTP_HOST} !^www\. [NC]
       RewriteCond %{HTTP_HOST} ^(.*)$  [NC]
       RewriteRule (.*) https://www.example.com/$1 [L,R=301]
    </IfModule>
    

    Why sometimes you don't need code %{HTTP_HOST} and use your domain exact url with www for the last line, because usually the result will have two www like:

    https://www.www.yourdomain.com
    

    Btw if you use cloudflare, just create pagerules redirect 301 all to https and www version.

    Create pagerules Fill with

    example.com/*
    

    Choose setting forward url then choose 301 and fill with this

    https://www.example.com/$1
    

    Save it and all will be redirected to https and www version.

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

    Sorry I don't have enough point to comment, but the rule of @prix will bring unneeded redirect.

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

    You can try http://domain.com/ on GTMETRIX and you will get this message

     "Avoid landing page redirects for the following chain of redirected URLs."
    
    http://domain.com/
    http://www.domain.com/
    https://www.domain.com/
    

    To prevent that, go to the first RewriteRule and add a "s" at the end of http. The new set of rule will look like this :

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

    Try it like this:

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

    The only real difference here is that first we redirect from non-WWW to WWW then we check for HTTPS and redirect it.

    If it does not work, try this one:

    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
    
    RewriteCond %{SERVER_PORT} !^443$
    RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301] 
    
    0 讨论(0)
提交回复
热议问题