Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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
</VirtualHost>

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

Debian & Apache 2

share|improve this question

closed as not a real question by Stefan Lasiewski, mdpc, Ward, MDMarra, Scott Pack Dec 20 '12 at 2:40

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

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? –  Shane Madden Dec 16 '12 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 '12 at 22:46
add comment

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]
</VirtualHost>
share|improve this answer
add comment

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.

share|improve this answer
add comment

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.)

share|improve this answer
add comment
up vote 1 down vote accepted

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.

share|improve this answer
add comment

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