skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    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.


  2. With your shown samples, please try following. Please make sure to clear your browser cache before testing your URLs.

    RewriteEngine ON
    ##Rules when .html file is not present in system.
    RewriteCond %{DOCUMENT_ROOT}/article/$2.html !-f [NC]
    RewriteRule ^([^-]*)-a-([0-9_]*).html/?$ news.php?article=$2&%{QUERY_STRING} [NC,L]
    
    ##Rules when .html file is present in system.
    RewriteCond %{DOCUMENT_ROOT}/article/$2.html -f [NC]
    RewriteRule ^([^-]*)-a-([0-9_]*).html/?$ article/$2.html [NC,L]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search