Hi All,

I'm trying to hide php extension using .htaccess and it works but, I also want to show custom error page when, somebody enters url which is not exist. The code is mentioned below :-

RewriteEngine on
# Rewrite /foo/bar to /foo/bar.php
RewriteRule ^([^.?]+)$ %{REQUEST_URI}.php [L]

# Return 404 if original request is /foo/bar.php
RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$"
RewriteRule .* - [L,R=404]

# NOTE! FOR APACHE ON WINDOWS: Add [NC] to RewriteCond like this:
# RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$" [NC]

ErrorDocument 404 /ap/errors/filenotfound.html

Right now my php extension is hidden but, custom page for 404 error is not appearing instead of that, it showing below error msg:-

**Object not found!

The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.

If you think this is a server error, please contact the webmaster.
Error 404
localhost
Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7 **

Thanks in Advance..

Dani AI

Generated

Brief summary and a safer, practical solution. was right that ForceType (making extensionless files be parsed as PHP) is a viable approach if pages are saved without .php, but that changes how static assets must be served. reached a working mod_rewrite flow but ran into Apache’s default “Object not found” because the error document was being missed or rewritten. The main causes are (a) the ErrorDocument path is wrong or not reachable, (b) the error page itself gets rewritten by the same rules, or (c) an external redirect is sent to a non‑existent URL instead of letting Apache serve the ErrorDocument. The snippet below is a compact, robust pattern to hide .php, let real files pass, keep the errors folder untouched, and allow the configured ErrorDocument to be used.

# place in site root (.htaccess); requires AllowOverride All
Options -MultiViews
RewriteEngine On

# let existing files/dirs (images, css, real pages) pass through
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# never touch the error-document path
RewriteCond %{REQUEST_URI} !^/errors/

# if <requested-path>.php exists, serve it internally
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L,QSA]

# if the browser explicitly asked for *.php, return 404 (let ErrorDocument handle it)
RewriteCond %{THE_REQUEST} \s/.*?\.php[?\s] [NC]
RewriteRule .* - [R=404,L]

# (document-root relative) example: /errors/filenotfound.html
ErrorDocument 404 /errors/filenotfound.html

Notes and troubleshooting

  • The ErrorDocument path must be correct (absolute from DocumentRoot) and the file readable; missing extension or wrong folder will produce the server’s default 404.
  • Excluding the errors folder prevents the ErrorDocument request from being rewritten into a loop. This is a common omission in earlier examples.
  • Avoid redirecting clients to the error file (don’t use a rule that does an external redirect to the error page); instead return the 404 status and let ErrorDocument serve the content.
  • Confirm .htaccess is active (AllowOverride All in the main Apache config) and inspect Apache logs (error.log) for rewrite/debug info. Testing with a header-only request (for example, curl -I http://localhost/nonexistent) quickly shows whether a 404 is coming from Apache or from a redirect.

This addresses the tradeoffs from ’s ForceType suggestion and the rewrite approach used by while avoiding common rewrite/404 pitfalls.

Recommended Answers

All 5 Replies

I use this:

<Files ~ "^[^\.]+$">
    ForceType application/x-httpd-php5
</Files>

It simply tells Apache that it should parse files without an extension as PHP. No longer the need to rewrite.

Thanks for your prompt reply. I am trying the above code directly in my .htacess file. Please find below :-

<IfModule mod_rewrite.c>
RewriteEngine on
<Files ~ "^[^\.]+$">
    ForceType application/x-httpd-php5
</Files>
</IfModule>

But, nothing is working. kindly, let me what exactly I need to do because I am new bie for .htacess. also add 404 custom error page redirect in the code.

Thanks in advance..

If you use a PHP file without extension, it'll work. Although now I'm doubting that's what you wanted.

Two things I wanted to achieve with my code which are as mentioned below :-
1) I want to hide .php extension &
2) I want to redirect user to custom error page when it encounters 404 error i.e. If some one enter wrong url then, it should redirect to custom error page i.e. ErrorDocument 404 /folder/errors/filenotfound.html

The above snippets give by you I tried but, its not hiding .php extension. I mean, I entered url without .php but, its doesn't display page.

Kindly, provide me full snippets which will take care of my both point. Also, let me know where I should place that code.

Thanks in advance..

I made my code working now, it take care of both i.e. hiding .php extension and redirecting to custom error page when some one enter wrong url. Also, for more security purpose I redirect it to custom error page when, someone enters url with .php extension. The code is mentioned below :-

RewriteEngine on

# Rewrite /foo/bar to /foo/bar.php
RewriteRule ^([^.?]+)$ %{REQUEST_URI}.php [L]

# Return 404 if original request is /foo/bar.php
RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$"

#below line code is for redierecting to custom error page when visitor enter url with .php extension
RewriteRule .* /folder_directory/errors/filenotfound [L,R]

#below line code is for redierecting to custom error page when visitor enter url which doesn't exists also it prevents visitors from accessing images or any architecture of website.
ErrorDocument 404 /folder_directory/errors/filenotfound
# NOTE! FOR APACHE ON WINDOWS: Add [NC] to RewriteCond like this:
# RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$" [NC]

Any one can use this code to solve the above purpose. This code need to be entered in .htaccess file.

I will keep this issue open for 2 more days for your comments & feedbacks. Later on, I will mark this issues as solved

Thanks again for helping me from your valuable time :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.