View previous topic :: View next topic |
Author |
Message |
[[Merlin]] Just Arrived


Joined: 16 Dec 2003 Posts: 3 Location: United Kingdom

|
Posted: Thu Jan 31, 2008 4:45 pm Post subject: Apache 2 - URL re-direct |
|
|
Hello
Does anybody know how to redirect all http traffic to https on Apache2. I tried this by using mod_rewrite without any success.
|
|
Back to top |
|
 |
Groovicus Trusted SF Member


Joined: 19 May 2004 Posts: 9 Location: Centerville, South Dakota

|
Posted: Thu Jan 31, 2008 4:52 pm Post subject: |
|
|
Can we maybe see what you tried so that we don't give advice that you already tried, or so we can see where you may have made an error, etc?
|
|
Back to top |
|
 |
[[Merlin]] Just Arrived


Joined: 16 Dec 2003 Posts: 3 Location: United Kingdom

|
Posted: Thu Jan 31, 2008 5:49 pm Post subject: |
|
|
Code: |
RewriteEngine On
RewriteRule http://(.*) https://$1 [L,R]
|
|
|
Back to top |
|
 |
capi SF Senior Mod


Joined: 21 Sep 2003 Posts: 16777097 Location: Portugal

|
Posted: Thu Jan 31, 2008 7:31 pm Post subject: |
|
|
Where are you placing this directive? At the main server configuration level, virtual-host level, directory level, inside a .htaccess...?
The RewriteRule pattern doesn't match against the full URL. It matches against the part of the URL after the hostname and port. So you'd want to match against ^/(.*) and add a condition with RewriteCond to check for %{HTTPS} being off.
This should do the trick:
Code: |
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/(.*)$ https://%{SERVER_NAME}/$1 [last,redirect] |
Note that if this is to be placed inside a .htaccess file, you need to remove the initial / so you get ^(.*)$
If this is for the whole site, you can also do it in a simpler way, by using mod_alias's Redirect directive:
Code: |
Redirect / https://www.example.com/ |
Just be careful, if you use Redirect, to only place it in the non-SSL virtual host, so that you don't get caught up in an infinite redirection loop (if the SSL host has that directive too, the SSL URL will get redirect to itself).
|
|
Back to top |
|
 |
[[Merlin]] Just Arrived


Joined: 16 Dec 2003 Posts: 3 Location: United Kingdom

|
Posted: Fri Feb 01, 2008 11:51 am Post subject: |
|
|
Hi capi,
I placed the following in virtual-host section of my apache configuration file - and it still doesn't work.
Code: |
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^/(.*)$ https://%{SERVER_NAME}/$1 [L,R]
|
I also tried using the redirect and it failed to have an effect.
Thanks for the help.
|
|
Back to top |
|
 |
capi SF Senior Mod


Joined: 21 Sep 2003 Posts: 16777097 Location: Portugal

|
Posted: Fri Feb 01, 2008 6:51 pm Post subject: |
|
|
Odd... That should be enough.
Exactly what happened? When you enter an http:// URL, it still goes to HTTP, right? Does HTTPS itself work? (e.g. if you enter an https:// URL)
You are loading mod_rewrite, right? Did you get any error messages? Check the error log (e.g. /var/log/apache2/error.log or wherever you're storing it).
|
|
Back to top |
|
 |
[[Merlin]] Just Arrived


Joined: 16 Dec 2003 Posts: 3 Location: United Kingdom

|
Posted: Mon Feb 04, 2008 1:00 pm Post subject: |
|
|
Hi capi,
When I type http:// it loads the standard error page, but for https:// it goes to the correct page.
I am loading mod_rewrite correctly as the error.log contains no errors for this module.
I think I may have set the server to accept only https connections and this prevents mod_write for receiving the http requests. I don't think its a problem with mod_rewrite as extensive googling come back with the exact solution you have kindly given me.
|
|
Back to top |
|
 |
[[Merlin]] Just Arrived


Joined: 16 Dec 2003 Posts: 3 Location: United Kingdom

|
Posted: Mon Feb 04, 2008 1:10 pm Post subject: |
|
|
I changed the Listen directive from allowing only HTTPS to accepting both HTTP and HTTPS.
Code: |
Listen 443
Listen 80
|
If I type http:// I get a blank page instead of the standard error page.
|
|
Back to top |
|
 |
|