Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

There's a few similar questions which I tried to work out the answer from, but so far I have been unsuccessful. Please advise how I can always redirect http to https (and also remove www. from the hostname in the process). Also a side note, it would be nice to do this inside the main Apache conf rather than .htaccess - but I imagine this will not apply to most people.


I've added this snippet to a VirtualHost section:

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

... but it has no effect when I access http://www.domain (it should redirect to https://domain)

Update 2:

It had no effect because I did not use RewriteEngine on - so it works now:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule ^.*$ https://%1/$1 [R=301,L]
share|improve this question

migrated from Feb 24 '10 at 12:24

This question came from our site for professional and enthusiast programmers.

marked as duplicate by Jenny D, peterh, MadHatter, Itai Ganot, Hyppy May 6 '15 at 12:50

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

What's wrong with all the other answers? How have you been unsuccessful? Also, there's pretty much no difference between doing this in .htaccess and your main Apache config file. – Christopher Feb 24 '10 at 12:02
When using in the main conf file: Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration – nbolton Feb 24 '10 at 12:04
What does your server configuration look like? – Gumbo Feb 24 '10 at 12:06
Snippet from a VirtualHost section: – nbolton Feb 24 '10 at 12:08
You might want to update your question with the details you've put in your comment about the error you're getting. – Andrew Aylett Feb 24 '10 at 12:13
up vote 3 down vote accepted

I wouldn't use mod_rewrite, you can achieve it simply with mod_alias:

Redirect permanent / https://other-site

Where 'other-site' is the hostname you want to redirect to, ommitting the www. prefix that you do not want.

share|improve this answer
Ok, this worked, but only for the VirtualHost *:80, so in *:443 I will try to use a snippet similar to my answer. – nbolton Feb 24 '10 at 12:45

There are so many solutions:

RewriteEngine On
RewriteCond %{HTTPS} !^on$
RewriteRule (.*) https://yourdomain/$1 [R,L]
share|improve this answer
I like this answer the best, because it's very concise on https. To extend it to work with any domain... RewriteCond %{HTTPS} !^on$ RewriteCond %{HTTP_HOST} ^(.*)$ [NC] RewriteRule ^.*$ https://%1%{REQUEST_URI} [L,R=301] – Mark Shust Jul 17 at 13:18

two solutions . add either of them to your .htaccess

RewriteEngine on
RewriteCondition %{SERVER_PORT} !^443$
RewriteRule ^(.*)$$1 [R=301,L] 

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$$1 [R=301,L]
share|improve this answer

If you're using a load balancer you'll need to use a different conditional. This works for AWS ELB:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteRule (.*)$1 [R=301,L]
share|improve this answer
I can confirm that this also works on the load balancer for – sameers Oct 30 '14 at 16:49

From your comments, it sounds like you're not including mod_rewrite:

LoadModule rewrite_module modules/
share|improve this answer
Ah, yep, you're right. Ok, now on to the question... – nbolton Feb 24 '10 at 12:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.