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

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

I tried different things such as:

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

(rewrite mod ON) or

NameVirtualHost *:80
<VirtualHost *:80>
   DocumentRoot /usr/local/apache2/htdocs
   Redirect permanent /secure

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.

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

I use this for the port 80 apache config:

<VirtualHost *:80>
    RewriteEngine On
    RewriteRule ^(.*)$$1 [R=301,L]
share|improve this answer

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

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

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