How to automatically redirect http to https in Apache

February 1, 2007

I will share 2 ways to achieve this:

  1. Using mod_rewrite. Add these directives to your configuration file:
  2. RewriteEngine On
    RewriteCond %{SERVER_PORT} !^443$
    RewriteRule ^/(.*) https://%{SERVER_NAME}/$1 [L,R]

  3. Using Redirect. Add these directives to your configuration file:

    SSLRequireSSL
    Redirect permanent /secure https://www.domain.com/secure

References:
Joseph Scott’s Blog
KarKomaOnline

Entry Filed under: LAMP. .

4 Comments Add your own

  • 1. Jenny Rurn  |  February 26, 2007 at 10:12 pm

    Very nice thanks!

    There is more examples at Apache SSL in htaccess examples in case different situation pops up! :)

    Reply
  • 2. Anil Kumar  |  October 16, 2008 at 7:34 pm

    I add this code in last of the htaccess file.URL chnage to https,but files are not opened.Please send reply as early as possible

    Reply
  • 3. Rob  |  January 24, 2009 at 2:44 am

    Cool, I was wondering about this. Thanks for posting.

    Reply
  • 4. ColtonCat  |  July 6, 2009 at 4:35 pm

    The second way (Redirect permanent) would be faster as mod_rewrite is not started and no regexes are checked?

    Reply

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Calendar

February 2007
M T W T F S S
« Sep   Nov »
 1234
567891011
12131415161718
19202122232425
262728  

Most Recent Posts