How to show custom 404 pages for URLs which has a file path of a php file

159 Views Asked by At

I have a custom 404.php file in the root folder. My .htaccess looks like:

Options +MultiViews

ErrorDocument 404 /404.php

When I visit URLs like localhost/blah, this 404 page loads correctly. But for urls like: localhost/departments/civil/blah it does not show the 404 page because I have a file with path /departments/civil.php. So Xampp is loading this civil.php file rather than the 404 file (which it should because of the /blah).

(Also, the civil.php file which is loaded has broken CSS links now as the href for CSS files in civil.php is ../css/civil.css, but since we have /blah in the url the path becomes localhost/departments/css/civil.css, rather than localhost/css/civil.css.)

How do I show the 404 page correctly for such URLs?

My folder structure:

|__/departments
|  |__civil.php
|
|__/css
|__|__civil.css
|
|__.htaccess
|
|__404.php

Also, don't know if this matters or not, but, I have set my DocumentRoot in xampp config file to my project name so that I don't have to go to localhost/<project-name> but only to localhost/ to view the project directly.

EDIT: I can use the $_SERVER['REQUEST_URI'] to see if there's anything after localhost/departments/civil, but I'll have to do this for all the php files I have. Is there any better option?

0

There are 0 best solutions below