I want to serve all requests for non-existing files via .php
.htaccess configuration:
ErrorDocument 404 /index.php
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
This does seem to mostly work but some specific file extensions (.txt,.jpg, some more?) get handled by apache instead of getting passed through php.
http://localhost:8000/home -> shows homepage
http://localhost:8000/file.zip (does not exist) -> shows custom styled 404 page served by PHP
http://localhost:8000/exists.txt (existing file) -> serves existing file
http://localhost:8000/doesnotexist.txt (does not exist) -> shows default apache 404 page (same as when no PHP is installed)
Is there some default apache handler for specific file extensions?
How can I set ALL requests to get passed through index.php?
Using docker php 7.4 apache as my base image
CodePudding user response:
I copied your experiment with Docker and the php 7.4-apache image. When opening the url http://localhost:8000/doesnotexist.txt I'm transfered to the index.php file as I would expect to be. The bottom three rewrite rules will rewrite everything to index.php.
First off: You have not posted a docker-compose.yml or Dockerfile, so to be sure: you have to enable the rewriting for Apache by either a command in the dockerfile or a custom Dockerfile like this:
# Dockerfile
FROM php:7.4-apache
RUN a2enmod rewrite
If you would not have enabled the rewrite, an error 500 should appear, because there are no if-conditions around the rewrite rule.
If you did enable the rewriting, the behaviour is still strange, because even if the line ErrorDocument 404 /index.php is not there the bottom rewrite should make that the index.php is served anyway. In other words: The bottom three lines in the .htaccess would make sure that no 404 is served, because everything is rewritten to the index.php.
When you say 'the default apache 404 page' are you talking about the 'Not found' page?
Not Found (H1 tag)
The requested URL was not found on this server.
Apache/2.4.52 (Debian) Server at localhost Port 8000
Try to remove and rebuild the container, so that no weird caching issue can give problems.
And what about your index.php. Is there anything in there that could trigger something strange? Could you post it here?
CodePudding user response:
You have a space between ^ and index.php in your RewriteRule, that space should not be present, I guess this is causing your error.
If I'm correct, you're not seeing the default 404 Error but the 500 Internal Server Error. Otherwise you might juste have made a typo when copying your code in your question.
Also I don't think this is the behavior you intended. You don't need these rewrite conditions to redirect to index.php when the filed is not found. You only need: ErrorDocument 404 /index.php
If what you are meaning to do is remove the .php at the end of the url, this should do it:
<IfModule mod_rewrite.c>
ErrorDocument 404 /index.php
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.] )$ $1.php [NC,L]
</IfModule>
