Search code examples
apache.htaccessmod-rewrite

Why does the following file does not exist condition not work in htaccess?


Don't know why the following rewrite rules do not work. The file-not-exist condition always triggers even though the file article/1.html file does exist. The requested URL is:

https://exampledomain.com/test-a-1.html

RewriteCond %{REQUEST_FILENAME} ^(.*)-a-([0123456789_]*)\.html$
RewriteCond %{DOCUMENT_ROOT}/article/%2.html !-f [NC,OR]
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^(.*)-a-([0123456789_]*)\.html$ news.php?article=$2&%{QUERY_STRING}
RewriteRule ^(.*)-a-([0123456789_]*)\.html$ /article/$2.html

If I comment out the !-f RewriteCond it nicely falls through to the second rule and accesses article/1.html. If this condition is active it never rewrites to article/1.html but always goes to news.php?article=1


Solution

  • I finally figured out the answer to my question. There is nothing wrong with my rewrite conditions or rules. They are exactly what they should be. However, %{DOCUMENT_ROOT} does not point to where the Apache documentation says it points to. On my Ionos managed server it points to /var/www/html. However, my actual document root is something like /kunden/homepages/... So the solution is to use the actual absolute path rather than %{DOCUMENT_ROOT}. Now everything works as it should.