Skip to content

Instantly share code, notes, and snippets.

@xiaohutai
Last active September 6, 2018 10:23
Show Gist options
  • Save xiaohutai/fbf33a0f8c9ed84c36982b278692c2ba to your computer and use it in GitHub Desktop.
Save xiaohutai/fbf33a0f8c9ed84c36982b278692c2ba to your computer and use it in GitHub Desktop.
.htaccess for no-www and yes-www
# -- no-www --
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# -- yes-www --
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} !^www [NC]
RewriteRule (.*) https://www.%{HTTP_HOST}/$1 [R=301,L]
@xiaohutai
Copy link
Author

Alternatives, may be:

RewriteCond %{ENV:HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment