为什么以下文件不存在条件在 htaccess 中不起作用?
Why does the following file does not exist condition not work in htaccess?
不知道为什么下面的重写规则不起作用。即使文件 article/1.html 文件确实存在,文件不存在条件也会始终触发。请求的 URL 是:
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=&%{QUERY_STRING}
RewriteRule ^(.*)-a-([0123456789_]*)\.html$ /article/.html
如果我注释掉 !-f RewriteCond,它会很好地落入第二条规则并访问 article/1.html。如果此条件处于活动状态,它永远不会重写为 article/1.html 但始终会转到 news.php?article=1
使用您显示的示例,请尝试执行以下操作。请确保在测试您的 URL 之前清除您的浏览器缓存。
RewriteEngine ON
##Rules when .html file is not present in system.
RewriteCond %{DOCUMENT_ROOT}/article/.html !-f [NC]
RewriteRule ^([^-]*)-a-([0-9_]*)\.html/?$ news.php?article=&%{QUERY_STRING} [NC,L]
##Rules when .html file is present in system.
RewriteCond %{DOCUMENT_ROOT}/article/.html -f [NC]
RewriteRule ^([^-]*)-a-([0-9_]*)\.html/?$ article/.html [NC,L]
我终于找到了问题的答案。我的重写条件或规则没有错。他们正是他们应该的。但是,%{DOCUMENT_ROOT}
并未指向 Apache 文档所说的位置。在我的 Ionos 托管服务器上,它指向 /var/www/html
。但是,我的实际文档根目录类似于 /kunden/homepages/...
所以解决方案是使用实际的绝对路径而不是 %{DOCUMENT_ROOT}
。现在一切正常。
不知道为什么下面的重写规则不起作用。即使文件 article/1.html 文件确实存在,文件不存在条件也会始终触发。请求的 URL 是:
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=&%{QUERY_STRING}
RewriteRule ^(.*)-a-([0123456789_]*)\.html$ /article/.html
如果我注释掉 !-f RewriteCond,它会很好地落入第二条规则并访问 article/1.html。如果此条件处于活动状态,它永远不会重写为 article/1.html 但始终会转到 news.php?article=1
使用您显示的示例,请尝试执行以下操作。请确保在测试您的 URL 之前清除您的浏览器缓存。
RewriteEngine ON
##Rules when .html file is not present in system.
RewriteCond %{DOCUMENT_ROOT}/article/.html !-f [NC]
RewriteRule ^([^-]*)-a-([0-9_]*)\.html/?$ news.php?article=&%{QUERY_STRING} [NC,L]
##Rules when .html file is present in system.
RewriteCond %{DOCUMENT_ROOT}/article/.html -f [NC]
RewriteRule ^([^-]*)-a-([0-9_]*)\.html/?$ article/.html [NC,L]
我终于找到了问题的答案。我的重写条件或规则没有错。他们正是他们应该的。但是,%{DOCUMENT_ROOT}
并未指向 Apache 文档所说的位置。在我的 Ionos 托管服务器上,它指向 /var/www/html
。但是,我的实际文档根目录类似于 /kunden/homepages/...
所以解决方案是使用实际的绝对路径而不是 %{DOCUMENT_ROOT}
。现在一切正常。