Search code examples
.htaccesshttp-status-code-404errordocument

404 page with htaccess


If i have a rewrite rule setup to get any name that is not a file and display the page-layout-select.php

How would I set a 404 if the page returns nothing as if I type anything (a page name that does not exist) it still loads the page-layout-select but without the content (because it doesn't exist)

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ page-layout-select.php?slug=$1 [L,QSA]

Any help with this would be great

Thanks


Solution

  • The way I do it (although I'm sure there are better ways) is to have a php include; if your normal pages are effectively:

    page-layout-select.php?page=news
    page-layout-select.php?page=home
    

    etc, where news, home are included (or written dynamically from a db), then your .htaccess can have

    ErrorDocument 404 /page-layout-select.php?page=404error
    

    where 404error is also just a simple include. In page-layout-select.php, check if the content you want to show exists, and if not, include 404error instead.