Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

My requirement looks Simple, But Googling Did not help me yet.
my query is
i want to Throw a 404 page to a user(Not Re-Direct to another folder or file), who is trying to Access *.php files in my website

ex: when a client asks for www.example.com/home/ i want to show the content , but when user says

www.example.com/home/index.php i want to show a 404 page.

i tried different methods, nothing worked for me, one of which tried is shown below

<Directory "C:/xampp/htdocs/*">
    <FilesMatch "^\.php">
        Order Deny,Allow
        Deny from all
        ErrorDocument 403 /test/404/
        ErrorDocument 404 /test/404/

Thanks in Advance

share|improve this question

migrated from stackoverflow.com Nov 21 '12 at 9:49

This question came from our site for professional and enthusiast programmers.

add comment

2 Answers

Just a basic rewrite rule, you can place this as .htaccess file in your folder or in your server config or virtual host config

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f

//allowed files, just remove the ! to use this rule to deny
RewriteCond %{REQUEST_URI} !.*\.(css|js|jpg|gif|png|zip|tff)

//redirect location
RewriteRule (.*) /public/www/404.php
share|improve this answer
This makes my server Just opposite to what is intended :) .. When i request with folder name it throws "Internal Server Error" and when i request folder/index.php it works fine –  Oxi Nov 20 '12 at 12:15
add comment
up vote 0 down vote accepted

this is my .htaccess file. you can demo it out here: http://mathpdq.com/demo2 or

AddDefaultCharset UTF-8
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /demo2
    RewriteCond %{HTTP_HOST} ^mathpdq\.com
    RewriteRule ^(.*)$ http://www.mathpdq.com/demo2/$1 [R=permanent,L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
share|improve this answer
This solution is working when i request the folder, but when i request folder/index.php instead of showing my 404 page it shows Not found error "The requested URL /test/index.php was not found on this server." also it shows "Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request." –  Oxi Nov 20 '12 at 13:08
@Oxi I modified my answer. in addition to what I think you are looking for it forces the url to have www at the beginning –  Drew Pierce Nov 20 '12 at 13:18
i am not using .htaccess, i added you code snippet with small modification into my Directory as shown below RewriteEngine On RewriteBase /test RewriteCond %{HTTP_HOST} ^localhost RewriteRule ^(.*)$ http://localhost/test/$1 [R=permanent,L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L] This gives me an infinite loop –  Oxi Nov 20 '12 at 13:54
lancelhoff.com/enabling-htaccess-in-apache-on-windows –  Drew Pierce Nov 20 '12 at 14:01
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.