Using the htaccess directive: ErrorDocument /filename.php sends automatically a 404 header.
If by any reason, which might mainly be for clean url (pretty url) handling, you would like to redirect non-existent files to a processing file mechanism without sending this 404 header, use the following:
Add this to your .htaccess
file:
1 2 3 4 5 6 7 |
RewriteEngine On RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^.*$ - [NC,L] RewriteRule ^.*$ index.php [NC,L] |
- enable rewrite
- check if requested file exists as a regualar file with size (not empty)
- check if requested file is link
- check if requested file is a directory
- if one of the previous 3 statements is true show that file
- otherwise go to index.php
If the redirect to index.php happens u can get the requested uri by using $_SERVER["REQUEST_URI"]
inside index.php
- ‘-d’ (is directory) Treats the TestString as a pathname and tests if it exists and is a directory.
- ‘-f’ (is regular file) Treats the TestString as a pathname and tests if it exists and is a regular file.
- ‘-s’ (is regular file with size) Treats the TestString as a pathname and tests if it exists and is a regular file with size greater than zero.
- ‘-l’ (is symbolic link) Treats the TestString as a pathname and tests if it exists and is a symbolic link.
- ‘-F’ (is existing file via subrequest) Checks if TestString is a valid file and accessible via all the server’s currently-configured access controls for that path. This uses an internal subrequest to determine the check, so use it with care because it decreases your servers performance!
- ‘-U’ (is existing URL via subrequest) Checks if TestString is a valid URL and accessible via all the server’s currently-configured access controls for that path. This uses an internal subrequest to determine the check, so use it with care because it decreases your server’s performance!