php - 错误文档结合 mod_rewrite

php - error document combined with mod_rewrite

我正在尝试使用 ErrorDocument 404 /404.php 设置 ErrorDocument 404 页面,但它不起作用,因为我启用了 mod_rewrite...是否有机会检查页面是否在跌至 mod_rewrite 之前是否存在?我在下面发布我的 htaccess ......这是由最后一条规则引起的,它将所有内容重定向到 product.php - RewriteRule ^(.+?)/?$ product.php?url= [L,QSA]

DirectoryIndex index.php
RewriteEngine On

ErrorDocument 404 /404.php

RewriteRule ^(admin|subdom)($|/) - [L]

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteRule ^coming-soon/?$ coming-soon.php [L]

RewriteRule ^tabulky-velikosti/?$ tabulky-velikosti.php [L]
RewriteRule ^o-bambusu/?$ o-bambusu.php [L]
RewriteRule ^vymena-zbozi/?$ vymena-zbozi.php [L]
RewriteRule ^doprava-a-platba/?$ doprava-a-platba.php [L]
RewriteRule ^obchodni-podminky/?$ obchodni-podminky.php [L]
RewriteRule ^ochrana-osobnich-udaju/?$ ochrana-osobnich-udaju.php [L]
RewriteRule ^o-nas/?$ onas.php [L]
RewriteRule ^kontakt/?$ kontakt.php [L]
RewriteRule ^faq/?$ faq.php [L]
RewriteRule ^kosik/?$ cart.php [L]
RewriteRule ^blog/?$ blog.php [L]
RewriteRule ^search/?$ search.php [L]

RewriteRule ^blog/tag/(.*)/? blog.php?tag= [L,QSA]
RewriteRule ^blog/(.*)/? blogDetail.php?url= [L,QSA]

RewriteRule ^search/(.*)/? search.php?s= [L,QSA]

RewriteRule ^zaciname/?$ category.php [L]
RewriteRule ^nakupovat/?$ category.php [L]
RewriteRule ^kategorie/?$ category.php [L]
RewriteRule ^prozkoumat/?$ category.php [L]

RewriteRule ^kategorie/((?:pan|dam|det)ske)-pradlo/?$ category.php?gender= [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ product.php?url= [L,QSA]

我能用它做什么吗?

还有另一种方法可以做到这一点。您通过 RewriteRule 设置 404,然后通过 URL 而不是文件设置 ErrorDocument

RewriteRule ^404/?$ 404.php
ErrorDocument 404 https://www.example.com/404.php

将它放在最后一条规则的下方,并从最后一条规则中删除 [L] 标志。

此方法不应导致mod_rewrite出现问题。确保在 测试此之前 清除缓存。

编辑

您可以在最后一条规则中添加条件以排除 404.php 页面吗?

RewriteCond %{REQUEST_URI} !^/404.php

这将阻止该页面发生重写,因此您的代码将是:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/404.php
RewriteRule ^(.+?)/?$ product.php?url= [L,QSA]