Redirect HTTP to HTTPS on default virtual host without ServerName


On my apache server I'd like to be able to redirect all incoming http requests to the equivalent https request. The catch is that I'd like to be able to do this for my default virtual host without specifying the ServerName and have the redirect work with whatever server name appeared in the request url. I'm hoping for something like this:

NameVirtualHost *:80
<VirtualHost *:80>
    RedirectPermanent / https://%{SERVER_NAME}/

Is this possible using Redirect or will I have to resort to Rewrite?

This question is tagged with apache http redirect https

~ Asked on 2012-07-23 21:47:52

The Best Answer is


Try adding this in your vhost config:

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

~ Answered on 2012-07-23 21:59:09


Both works fine. But according to the Apache docs you should avoid using mod_rewrite for simple redirections, and use Redirect instead. So according to them, you should preferably do:

<VirtualHost *:80>
    Redirect /

<VirtualHost *:443>
    # ... SSL configuration goes here

The first / after Redirect is the url, the second part is where it should be redirected.

You can also use it to redirect URLs to a subdomain: Redirect /one/

~ Answered on 2015-01-16 03:30:04

Most Viewed Questions: