Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mortenege/33d93c34f300991f6a61ad248b2d96da to your computer and use it in GitHub Desktop.
Save mortenege/33d93c34f300991f6a61ad248b2d96da to your computer and use it in GitHub Desktop.
HTTPS and www Redirect using .htaccess in Apache

This post describes how to enforce a secure https connection using the .htaccess-file in Apache. At the same time we would like to redirect all www-requests to a non-www secure request. This post is a concatenation of the following forum posts here and here. Edit .htaccess with the following content.

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

Now requests to http://www.mydoamin.com, http://mydomain.com and https://www.mydomain.com will be redirected to https://mydomain.com.

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