PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, February 6, 2022

[FIXED] Apache uses it's own 404 handler instead of index.php for .txt files

 February 06, 2022     apache, php     No comments   

Issue

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


Solution

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?



Answered By - Jos Faber
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing