3

This Apache reference page and this wiki clearly states that to achieve simple http to https redirects, mod_alias should be used instead of the more expensive mod_rewrite.

I have an Apache virtualhost which uses a wildcard to match multiple subdomains. I need all these subdomains redirected to their corresponding https counterparts. Currently, I am using the following (with mod_rewrite):

<VirtualHost *:80>
    ServerName lvh.me
    ServerAlias *.lvh.me

    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]
</VirtualHost>

I tried the following, but it did not work.

<VirtualHost *:80>
    ServerName lvh.me
    ServerAlias *.lvh.me

    Redirect temp / https://%{HTTP_HOST}/
</VirtualHost>

The apache variable HTTP_HOST doesn't seem to be recognized in redirect directives.

So, is there any other way to use mod_alias to achieve the above effect?

Anjan
  • 277
  • 1
  • 2
  • 14

2 Answers2

3

You cannot. Redirect is able to handle simple redirections, where you're sending the client to a single, specific name, but does not have the ability to do complex substitutions (setting aside the fact that %{HTTP_HOST} is mod_rewrite-specific).

Just stick with mod_rewrite. mod_alias isn't capable of doing what you need.

Shane Madden
  • 112,982
  • 12
  • 174
  • 248
  • I suspected that `HTTP_HOST` is `mod-rewrite` specific. But I couldn't get any info about it. All I found that `HTTP_HOST` is an ordinary Apache variable. – Anjan Aug 09 '12 at 09:06
1

As a followup, if you use Apache 2.4.19 or later, you can use a simpler format of Redirect within <Location> sections as detailed in the mod_alias documentation. This new format uses expression syntax, allowing the use of variables.

For example:

<VirtualHost *:80>
    ServerName lvh.me
    ServerAlias *.lvh.me

    <Location "/">
        Redirect "https://%{SERVER_NAME}%{REQUEST_URI}"
    </Location>
</VirtualHost>

Note that only the Redirect directive works with this shorter format, RedirectPermanent etc. do not.