Redirect non-www to www in .htaccess

后端 未结 13 2041
轻奢々
轻奢々 2020-11-22 05:51

I have this in my .htaccess file:

RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) http://www.example.com$1 [R=301,L]

but whenever I

13条回答
  •  忘掉有多难
    2020-11-22 06:10

    I have tested all the above solutions but not working for me, i have tried to remove the http:// and won't redirect also removed the www it redirect well, so i get confused, specially i am running all my sites under https://

    So i have combined some codes together and came up with perfect solution for both http:// and https:// and www and non-www.

    # HTTPS forced
    
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
    # Redirect to www
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
    
    

    Hope this can help someone :)

提交回复
热议问题