How to redirect from www to https www with htacces?

后端 未结 4 1782
北恋
北恋 2021-01-21 15:42

I need to do the following: My current address looks like: https://www.domain.com

I want to redirect with htaccess: www.domain.com TO https://www.domain.com and http:/

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-21 16:22

    You should have a ! infront of your domain name condition:

    ### Redirect non-www => www ###
    RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
    RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
    

    Because you want to say, do the next rule, if this condition matches... and the condition should be host does NOT match what you want.

    If i mix this with techniques i have seen and used before, you could try this:

    ### Redirect non-www => www ###
    RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
    RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
    
    # redirect urls with index.html to folder
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index.html HTTP/
    RewriteRule ^(([^/]+/)*)index.html$ https://%{HTTP_HOST}/$1 [R=301,L]
    
    # change // to /
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)//(.*) HTTP/ [NC]
    RewriteRule ^.*$ https://%{HTTP_HOST}/%1/%2 [R=301,L]
    

提交回复
热议问题