3

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?

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.

  • That's similar, but is more about mod_alias verses mod_rewrite, and appears out of date. %{HTTP_HOST} is a mod_rewrite thing. Apache 2.2 didn't support many of these Apache variables back then. – Stefan Lasiewski Mar 3 '16 at 22:48
  • Ah, perhaps this information from Apache's documentation is more helpful: First, all Redirects are processed before Aliases are processed, and therefore a request that matches a Redirect or RedirectMatch will never have Aliases applied. Second, the Aliases and Redirects are processed in the order they appear in the configuration files, with the first match taking precedence. I think this is saying you can't use Apache variables because the statement is evaluated in the wrong order. Please let me know if I'm misreading. – sippybear Mar 4 '16 at 7:43
  • The referenced document is available here:httpd.apache.org/docs/current/mod/mod_alias.html – sippybear Mar 4 '16 at 7:48
  • But Apache variables are a part of the core Apache code, and are not part of mod_rewrite. mod_rewrite has it's own set of parameters, like %{HTTP_HOST}, which I believe are different. In my case, I'm not using mod_rewrite at all, just mod_alias and the Apache core code. – Stefan Lasiewski Mar 4 '16 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.