Apache 2.4 – Execution Order of mod_alias, mod_rewrite, and mod_proxy

apache-2.4mod-aliasmod-proxymod-rewriterewrite

I can't find any documentation on Apache vhosts instructions priority / execution order between

Is there any documentation out there about the order these are going to be handled by Apache? Which one takes precedence over the other?

Best Answer

The general rule is that the order of the directives is important. The directive that comes first and matches wins (just like the order of the VirtualHost definitions – the first VHost is the default VHost).

In most cases it should work fine if you define Aliases/Redirects and RewriteRules first and proxy rules like ProxyPass last if you want to let redirection take place before your proxy settings apply.


http://httpd.apache.org/docs/2.4/mod/mod_alias.html#order

Aliases and Redirects occurring in different contexts are processed like other directives according to standard merging rules. But when multiple Aliases or Redirects occur in the same context (for example, in the same <VirtualHost> section) they are processed in a particular order.

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.

For this reason, when two or more of these directives apply to the same sub-path, you must list the most specific path first in order for all the directives to have an effect.


https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewriterule

The RewriteRule directive is the real rewriting workhorse. The directive can occur more than once, with each instance defining a single rewrite rule. The order in which these rules are defined is important - this is the order in which they will be applied at run-time.

Also see: Apache mod_rewrite Technical Details


https://httpd.apache.org/docs/2.4/mod/mod_proxy.html#proxypass

Ordering ProxyPass Directives
The configured ProxyPass and ProxyPassMatch rules are checked in the order of configuration. The first rule that matches wins. So usually you should sort conflicting ProxyPass rules starting with the longest URLs first. Otherwise, later rules for longer URLS will be hidden by any earlier rule which uses a leading substring of the URL. Note that there is some relation with worker sharing.

Ordering ProxyPass Directives in Locations
Only one ProxyPass directive can be placed in a Location block, and the most specific location will take precedence.

Exclusions and the no-proxy environment variable
Exclusions must come before the general ProxyPass directives. In 2.4.26 and later, the "no-proxy" environment variable is an alternative to exclusions, and is the only way to configure an exclusion of a ProxyPass directive in Location context. This variable should be set with SetEnvIf, as SetEnv is not evaluated early enough.

Related Topic