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..

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.