• RSS
  • Twitter
  • FaceBook

Security Forums

Log in

FAQ | Search | Usergroups | Profile | Register | RSS | Posting Guidelines | Recent Posts

Apache 2 - URL re-direct

Users browsing this topic:0 Security Fans, 0 Stealth Security Fans
Registered Security Fans: None
Post new topic   Reply to topic   Printer-friendly version    Networking/Security Forums Index -> UNIX // GNU/Linux

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


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

Offline

PostPosted: Thu Jan 31, 2008 4:45 pm    Post subject: Apache 2 - URL re-direct Reply with quote

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
View user's profile Send private message
Groovicus
Trusted SF Member
Trusted SF Member


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

Offline

PostPosted: Thu Jan 31, 2008 4:52 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
[[Merlin]]
Just Arrived
Just Arrived


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

Offline

PostPosted: Thu Jan 31, 2008 5:49 pm    Post subject: Reply with quote

Code:

RewriteEngine On
RewriteRule http://(.*) https://$1 [L,R]
Back to top
View user's profile Send private message
capi
SF Senior Mod
SF Senior Mod


Joined: 21 Sep 2003
Posts: 16777097
Location: Portugal

Offline

PostPosted: Thu Jan 31, 2008 7:31 pm    Post subject: Reply with quote

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
View user's profile Send private message
[[Merlin]]
Just Arrived
Just Arrived


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

Offline

PostPosted: Fri Feb 01, 2008 11:51 am    Post subject: Reply with quote

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
View user's profile Send private message
capi
SF Senior Mod
SF Senior Mod


Joined: 21 Sep 2003
Posts: 16777097
Location: Portugal

Offline

PostPosted: Fri Feb 01, 2008 6:51 pm    Post subject: Reply with quote

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
View user's profile Send private message
[[Merlin]]
Just Arrived
Just Arrived


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

Offline

PostPosted: Mon Feb 04, 2008 1:00 pm    Post subject: Reply with quote

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
View user's profile Send private message
[[Merlin]]
Just Arrived
Just Arrived


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

Offline

PostPosted: Mon Feb 04, 2008 1:10 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   

Post new topic   Reply to topic   Printer-friendly version    Networking/Security Forums Index -> UNIX // GNU/Linux All times are GMT + 2 Hours
Page 1 of 1


 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Community Area

Log in | Register