I have a website that I want to be access only by https://myurl.com. A normal typing myurl.com should be forwarded to the https.

I tried different things such as:

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://myurl.com/$1 [R,L] 

(rewrite mod ON) or

NameVirtualHost *:80
<VirtualHost *:80>
   ServerName mysite.example.com
   DocumentRoot /usr/local/apache2/htdocs
   Redirect permanent /secure https://mysite.example.com/secure

But they didnt work, which is the right way to do it?

Debian & Apache 2

  • 2
    Can you expand on what you mean by "didn't work"? Where did you put that mod_rewrite configuration? What did the browser say when you tried to access the redirect? Can you provide the output from apachectl -S? Dec 16, 2012 at 18:36
  • Yep, sorry, if you browse the 80 you do not see anything (I still have no 404). If you browse the https everything works. I put the mod_rewrite always in the virtualHost config. I will provide the output for tomorrow..
    – elect
    Dec 16, 2012 at 22:46

4 Answers 4


I use this for the port 80 apache config:

<VirtualHost *:80>
    ServerName mysite.example.com
    RewriteEngine On
    RewriteRule ^(.*)$ https://mysite.example.com$1 [R=301,L]

In addition to redirecting from HTTP to HTTPS, I recommend that you also use HSTS and set the secure flag on all cookies. This will tell browsers to always connect over HTTPS (not HTTP) and to send the cookies only over HTTPS (not HTTP).

Useful: a description of how to set HSTS in your Apache config.


You can put this in your regular HTTP (e.g. *:80) vhost def. (There is no need to test the %{SERVER_PORT} or %{HTTPS} variables when this is only in the HTTP vhost def.)

This is a bit more portable than the previous answers, and won't get tripped up by different hostnames due to DNS search domains, CNAMES, etc.:

RewriteEngine On
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

(But the suggestion to enable HSTS is also a good one.)


So, essentially no one of the methods was working because I accidentally disabled apache from listening on 80... Sorry for that, my fault.

Now also a simply

Redirect permanent / https://mysite

is working

However +1 to Paul because also his suggestion works and to Keith and D.W. for the interesting additional information.

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