Remove PHP extension using Apache Mod Rewrite .htaccess

MeridianManagement
MeridianManagement used Ask the Experts™
on
I want mydomain.com/home to translate to mydomain.com/home.php. At the same time, I would like anything that points to a PHP file to be redirected to show without the php extension.

Thanks!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
It's not quite what you are looking for, but if you are using apache, you can modify your httpd.conf by changing the AddType line:

AddType application/x-httpd-php .php .html

(add the .html to the end of the line)

This will process html files through the php engine.  It does not remove the extension from the address bar, but it would allow files with .html extensions to actually have .php.  This has the advantage that it is not obvious to users that a site is using php, but it might slow your server down a bit as all html files would then be processed through the PHP engine, whether they contained PHP code or not.

You could really come up with any extension and pass it this way.

HTH - flub

Avinash ZalaWeb Expert

Commented:
try this rule in your htaccess file


RewriteRule ^([a-zA-Z-_]+)$ $1.php [NC,L]
RewriteRule ^([a-zA-Z-_]+)/$ $1.php [NC,L]


This will work as below:

If you write like this : http://www.domain.com/article then it loads the content of http://www.domain.com/article.php 

Same http://www.domain.com/testimonial will load http://www.domain.com/testimonial.php and so on.

Hope this helps
Addy

Commented:
Just to add on to Addy, if you have mod_rewrite on and you want this applied to the whole site, it could be in the httpd.conf.  Otherwise, add that code in the htaccess and make sure that you have the appropriate AllowOverrides enabled in the httpd.conf file.

  - flub
Top Expert 2010

Commented:
The first part is fairly simple e.g.

RewriteBase  /
RewriteCond %{REQUEST_FILENAME} !-f         # Not an existing file
RewriteCond %{REQUEST_FILENAME} !-d        # Not an existing directory
RewriteCond %{REQUEST_FILENAME}\.php -f   # BUT a .php file exists for the name
RewriteRule  ^[^.]*$   %{REQUEST_URI}\.php  [T=application/x-httpd-php]

Second part isn't really practicable, as without proxying the request to the same server a rewrite isn't going to work, it will just loop.

But if you want to remove all the .php extentions from your existing files, and add something along the following lines to your .htaccess you should be able to achieve what you want:

<FilesMatch "^.*$">
    ForceType application/x-httpd-php
</FilesMatch>
Top Expert 2010
Commented:
[Attempt 2, correcting a typo]

The first part is fairly simple e.g.

RewriteBase  /
RewriteCond %{REQUEST_FILENAME} !-f         # Not an existing file
RewriteCond %{REQUEST_FILENAME} !-d        # Not an existing directory
RewriteCond %{REQUEST_FILENAME}\.php -f   # BUT a .php file exists for the name
RewriteRule  ^[^.]*$   %{REQUEST_URI}\.php  [T=application/x-httpd-php]

Second part isn't really practicable, as without proxying the request to the same server a rewrite isn't going to work, it will just loop.

But if you want to remove all the .php extentions from your existing files, and add something along the following lines to your .htaccess you should be able to achieve what you want:

<FilesMatch "^[^.]*$">
    ForceType application/x-httpd-php
</FilesMatch>

Author

Commented:
aRober, I don't know if you tried the code in the first part, but it created a 500 internal server error on my site

Author

Commented:
addywatson, could not get your code to work either

Author

Commented:
arober, here's your code combined w/ the existing htaccess. Again it's causing a 500 internal server error when I add your section.
AuthType Basic
AuthName "rapid"
AuthUserFile "/path/to/passwd"
require valid-user


RewriteBase  /
RewriteCond %{REQUEST_FILENAME} !-f         # Not an existing file
RewriteCond %{REQUEST_FILENAME} !-d        # Not an existing directory
RewriteCond %{REQUEST_FILENAME}\.php -f   # BUT a .php file exists for the name
RewriteRule  ^[^.]*$   %{REQUEST_URI}\.php  [T=application/x-httpd-php]

Open in new window

Avinash ZalaWeb Expert

Commented:
you have to write

RewriteEngine On

RewriteBase /

before my code.

And is your mod_rewrite module is enabled in you apache htpd.conf file?

Just check these few things.

Hope this helps
Addy

Author

Commented:
Yes, it is enabled because I have wordpress on the same server and it is a dedicated server

Author

Commented:
Still causing an error
AuthType Basic
AuthName "rapid"
AuthUserFile "/path/to/passwd"
require valid-user

RewriteEngine On
RewriteBase  /
RewriteCond %{REQUEST_FILENAME} !-f         # Not an existing file
RewriteCond %{REQUEST_FILENAME} !-d        # Not an existing directory
RewriteCond %{REQUEST_FILENAME}\.php -f   # BUT a .php file exists for the name
RewriteRule  ^[^.]*$   %{REQUEST_URI}\.php  [T=application/x-httpd-php]

Open in new window

Avinash ZalaWeb Expert

Commented:
RewriteEngine On
RewriteBase  /

RewriteRule ^([a-zA-Z-_]+)$ $1.php [NC,L]
RewriteRule ^([a-zA-Z-_]+)/$ $1.php [NC,L]


Try this code

Hope this helps
Addy

Author

Commented:
Thanks! I had to remove the comments and then it seemed to work. I guess you can't do a comment on the same line as the code.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial