4

The Apache HTTP Server's mod_alias provides the Redirect directive which is great for simple redirects, like the following:

# Redirect login to https
Redirect permanent /login https://www.example.org/login

While many admins use mod_rewrite for redirects, the Apache documentation at When not to use mod_rewrite suggest that in general, mod_alias is preferred over mod_rewrite. I'd like to use mod_alias more.

Can I use Redirect with Apache variables such as ServerName? For example, to enforce that certain content is available over HTTPS only, I would want to do something like this:

Redirect permanent /login https://%{SERVER_NAME}/login
Redirect permanent /special-project-1 https://%{SERVER_NAME}/special-project-1
Redirect permanent /special-project-2 https://%{SERVER_NAME}/special-project-2

Neither of these work, and the literal string %{SERVER_NAME} is printed in the response:

host% curl http://www.example.org/login
...
302 Found
The document has moved <a href="https://%{SERVER_NAME}/login">here</a>.

Are Apache variables allowed with mod_alias directives? Are the variables available using a different syntax?

Stefan Lasiewski
  • 24,361
  • 42
  • 136
  • 188

2 Answers2

1

A bit late, but ran across this thread looking w/ just this problem and found a solution.

With Apache 2.4 you can't do:

Redirect permanent /login https://%{SERVER_NAME}/login

But you can do:

<Location /login>
  Redirect permanent https://%{HTTP_HOST}/login
</Location>
0

I believe a similar question is answered here: https://serverfault.com/a/415796/337307

@shanemadden wrote:

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.

sippybear
  • 3,367