HowTo redirect HTTP to HTTPS on the same httpd?

10,711

Solution 1

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

Solution 2

You can use mod_alias and Redirect based on directory. The linked document identifies additional details.

<Directory /path/to/site>
   Redirect /service https://foo2.example.com/service 
</Directory>
Share:
10,711

Related videos on Youtube

mosg
Author by

mosg

/* May all your PUSHs be POPed */

Updated on September 17, 2022

Comments

  • mosg
    mosg almost 2 years

    Here is what I have got:

    • CentOS 5.4 (32-bit)
    • installed Apache httpd (Server version: Apache/2.2.11 (Unix))
    • mod_rewrite already presents

    Question: how to redirect simple http://site.com to https://site.com not using VirtualHost defines?

    PS: tried to find in later answers on SF, but doesn't find nice solution.

    Thanks.

  • cregox
    cregox about 13 years
    RewriteRule ^(.*) https://%{HTTP_HOST}$1 makes more sense to me.
  • Ernest Mueller
    Ernest Mueller about 13 years
    Won't that infinite loop on you?
  • cregox
    cregox about 13 years
    Nope, just tested it. It's even how it's advised under the docs - look at the end of it.
  • Purplejacket
    Purplejacket almost 10 years
    I get an infinite loop using Apache 2.4.10, deploying on Heroku, when I insert those commands into my .htaccess. And the variant suggested by Cawas oddly mangles my URL. I've tried a different set of commands: RewriteCond %{HTTP:X-Forwarded-Proto} !https and RewriteRule ^/?(.*) https://%{SERVER_NAME}%{REQUEST_URI} -- which works on Chrome and Firefox, but fails on Safari and Opera. Interestingly, if I take the three lines above and omit RewriteEngine On I no longer get the infinite loop, but it still fails in Safari and Opera (and works in Chrome and Firefox).