0

I have several RewriteRule inside <VirtualHost>, inside the <Directory> directive which belongs to the web root of said <VirtualHost> and inside .htaccess of that directory.

Question: In what order does Apache 2.4 evaluate those RewriteRules?

I tried to read Apache Docs - How the sections are merged, but I am lost and I am not sure if the rules given in that section apply to RewriteRule.

My simplified Apache config looks like this

<VirtualHost *:80>
  ServerName www.my-domain.tld
  ServerAdmin [email protected]
  DocumentRoot /var/www/htdocs

  RewriteEngine on
  RewriteRule "^/something" "/somewhat"
  RewriteRule "^/something-else" "/foo"
</VirtualHost>

<Directory "/var/www/htdocs">
  Require all granted
  AllowOverride All
  Options FollowSymLinks

  RewriteEngine on
  RewriteRule "^something" "nothing"
  RewriteRule "^something-else" "bar"

  DirectoryIndex index.html
</Directory>

and the .htaccess

  RewriteEngine on
  RewriteRule "^foo" "bar"
  RewriteRule "^nothing" "something"

In particular, the following aspects confuses me:

  • .htaccess is said to overwrite <Directory>, does this mean that any RewriteRule inside <Directory> is completely ignored as if it was not there at all as soon as there is a single RewriteRule inside .htaccess?
  • Sections inside <VirtualHost> are said to be evaluated after any other section in order to allow overwriting. IMHO, this statement does not make sense in case of RewriteRule (or is at least confusing), because an earlier rewrite rule "wins".
4
  • 1
    As a general rule - do not use .htacess when you have full admin rights and access to Apache main server configuration and set AllowOverride None. Support for .htaccess is intended for shared hosting where web masters don't have access to the main server config. See httpd.apache.org/docs/trunk/howto/htaccess.html#when - When you do need .htaccess then simply put all your configuration in .htaccess files and don't use both. Inconsistency will lead to unnecessary complexity as you're already experiencing.
    – HBruijn
    Nov 17 at 13:09
  • The technical details are in the manual here httpd.apache.org/docs/2.4/rewrite/tech.html and not for the faint of heart.
    – HBruijn
    Nov 17 at 13:15
  • I know that using the configuration file is beneficial, not at last because it is faster as the configuration file is only read and cached once, while .htaccess is read on every request. But while I myself have access to the Apache config as I am the server admin, I provide web space to other uses and they have to use .htaccess. So I try to figure out how everything plays together. Nov 17 at 20:21
  • just to clarify @user2690527 .htaccess is read "several times" for every request, not just once Nov 22 at 21:09

0

You must log in to answer this question.

Browse other questions tagged .