Skip to content

Instantly share code, notes, and snippets.

@kulbakin
Created February 8, 2016 07:00
Show Gist options
  • Save kulbakin/df1e394161956c3a0041 to your computer and use it in GitHub Desktop.
Save kulbakin/df1e394161956c3a0041 to your computer and use it in GitHub Desktop.
Common apache .htaccess redirects
RewriteEngine On
# force www
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* http://www.%{HTTP_HOST}/$0 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}/$0 [R=301,L]
# force no www
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule .* http://%1/$0 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule .* https://%1/$0 [R=301,L]
# no trailing slashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment