# [Solved again]Redirecting http => https on apache

## Letharion

On this page: http://www.cyberciti.biz/tips/howto-apache-force-https-secure-connections.html

Two different methods are listed that redirect traffic from http to https.

```
Redirect permanent / https://mail.nixcraft.com/
```

and

```
RewriteEngine On

RewriteCond %{HTTPS} off

RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

LoadModule rewrite_module modules/mod_rewrite.so
```

I fail to see the difference between them.

I'm guessing that the second one is more flexible, but I'm not sure.

Probably, the first solution is what I'm looking for because I just want all traffic redirected.

Could anyone shed some light on when/why one would want to use the second solution over the first?

Are there other ways?Last edited by Letharion on Tue Mar 24, 2009 8:45 am; edited 3 times in total

----------

## alex.blackbit

the second code snippet is completely generic. this is what you should use.

you only get redirected if the given url was not already https.

but i believe the mod_rewrite module should be loaded before doing that.

----------

## Letharion

 *alex.blackbit wrote:*   

> the second code snippet is completely generic. this is what you should use.
> 
> you only get redirected if the given url was not already https.
> 
> but i believe the mod_rewrite module should be loaded before doing that.

 

Ah, so the first redirects even if I go to https from the beginning. That's "wrong" I suppose.

But how is the first "not generic"?

Yes, it probably should. My mistake.

----------

## alex.blackbit

with "generic" i meant that no actual URL is included in the statement.

you can take these few lines and copy/paste it to other config files or even use it in a file included in other files.

----------

## Letharion

Hmm, I now have

```
RewriteEngine On

RewriteCond %{HTTPS} off

RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
```

In httpd.conf, after 

```
LoadModule rewrite_module modules/mod_rewrite.so
```

Apache config test return OK.

But I don't get redirected.

Why?

----------

## h0mer`-

how did u solve it?

----------

## Letharion

 *h0mer`- wrote:*   

> how did u solve it?

 

I haven't. I'm sure it did work, but now it doesn't. The [Solved] is "old".

My best guess is that I've changed something else, somewhere else, that affects this, but I don't know what. I'm not very good with apache.Last edited by Letharion on Tue Feb 17, 2009 3:56 pm; edited 1 time in total

----------

## tgurr

For me the following works just fine:

```
    RewriteEngine On

    RewriteCond %{SERVER_PORT} !443

    RewriteRule (.*) https://subdomain.domain.de/ [R]
```

----------

## Letharion

 *tgurr wrote:*   

> For me the following works just fine:
> 
> ```
>     RewriteEngine On
> 
> ...

 

I tried that, but it still doesn't have any effect.

Could it matter where you have that code? Where is it in your files?

----------

## alex.blackbit

tgurr, does the more general way work for you too?

----------

## jon123

I have a minor difference with my configuration.  This one grabs all the URL information after the domain name and appends it to the rewrite at $1.

```

RewriteCond  %{SERVER_PORT} !^443$

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

```

The others look fine as well.  I guess my question is where is your RewriteRule?  Do you have virtual host directives?

For example Is your rewrite rule inside of <VirtualHost *:80> If so you need to remove the RewriteCond  :Smile: 

----------

## xbmodder

What you really need is a 426, upgrade required. Check out the RFC: http://www.ietf.org/rfc/rfc2817.txt

----------

## Letharion

 *xbmodder wrote:*   

> What you really need is a 426, upgrade required. Check out the RFC: http://www.ietf.org/rfc/rfc2817.txt

 

Interesting. From the RFC I get the feeling that a 426 is supposed to be some sort of "best practice" (Haven't read the full document TBH), but googling for a apache setup, I

find very little usage of 426. Even a quick search in the apache docs doesn't reveal anything.

Any idea what that is?

----------

## Letharion

 *jon123 wrote:*   

> I have a minor difference with my configuration.  This one grabs all the URL information after the domain name and appends it to the rewrite at $1.
> 
> ```
> 
> RewriteCond  %{SERVER_PORT} !^443$
> ...

 

I had it directly in my httpd.conf, after all the module loading

I moved it into <VirtualHost *:80> and removed the Cond as you instructed, and now it works.

Is there any prefered place where it should be?

----------

## Mad Merlin

The first method only works for one page, nothing else gets redirected. You could in fact still use http://mail.nixcraft.com/index.php and not be redirected (or any other URL, for that matter).

----------

## Letharion

 *Mad Merlin wrote:*   

> The first method only works for one page, nothing else gets redirected. You could in fact still use http://mail.nixcraft.com/index.php and not be redirected (or any other URL, for that matter).

 

Thanks  :Smile:  I basically need to read up on Apache configs  :Wink: 

----------

