php.htaccessmod-rewriteerrordocument

php - error document combined with mod_rewrite


I'm trying to set ErrorDocument 404 page by using ErrorDocument 404 /404.php, but it does not work, because I have mod_rewrite enabled... Is there some chance to check, if the page exist before it falls to mod_rewrite? I'm posting my htaccess down bellow... It is caused by last rule, which redirects everything to product.php - RewriteRule ^(.+?)/?$ product.php?url=$1 [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=$1 [L,QSA]
RewriteRule ^blog/(.*)/? blogDetail.php?url=$1 [L,QSA]

RewriteRule ^search/(.*)/? search.php?s=$1 [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=$1 [L,QSA]

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

Is there anything I can do with that?


Solution

  • There is another way in which you can do this. You set the 404 via a RewriteRule and then set the ErrorDocument via URL instead of the file:

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

    Place it below your last rule and remove the [L] flag from your last rule.

    This method shouldn't cause an issue with mod_rewrite. Make sure you clear your cache before testing this.

    EDIT

    You could just add a condition to the last rule to exclude the 404.php page?

    RewriteCond %{REQUEST_URI} !^/404.php
    

    That would stop the Rewrite from happening for that page, so your code would be:

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