• Unfortunately, we have experienced significant hard drive damage that requires urgent maintenance and rebuilding. The forum will be a state of read only until we install our new drives and rebuild all the configurations needed. Please follow our Facebook page for updates, we will be back up shortly! (The forum could go offline at any given time due to the nature of the failed drives whilst awaiting the upgrades.) When you see an Incapsula error, you know we are in the process of migration.

.htaccess not removing .php extension?

R.I.P Millercent
Loyal Member
Joined
Nov 6, 2012
Messages
2,230
Reaction score
314
Hi,

I have a problem with my .htaccess file. It's removing .html extensions fine, how ever I can't get it to remove .php extensions from links. For example, instead of going to siteurl.com/client/order.php I want it to be siteurl.com/client/order

This is what I have in my .htaccess file;
PHP:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php

But sadly it's not quite working. It worked fine on the old server I hosted my site on, how ever it's not quite working on my new one. Anyone have any ideas?
 
R.I.P Millercent
Loyal Member
Joined
Nov 6, 2012
Messages
2,230
Reaction score
314
Allowoverride is enabled and set to all (within my public_html folder) - Restarted Apache to update it and no errors were logged so that should be fine but still isn't resolving.

Tried a few different ways in coding the removal of the .php extension and none were successful yet
 
Joined
Dec 26, 2006
Messages
1,305
Reaction score
167
Try the following:

Code:
RewriteEngine On


RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]


RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1html -f
RewriteRule ^(.+?)/?$ /$1.html [L]
 
Custom Title Activated
Loyal Member
Joined
Mar 26, 2012
Messages
1,465
Reaction score
131
Did you restart your web server after you made changes to the htaccess?

Also don't know if this helps but, here is my old version before I started to use IIS

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ /$1.php [L,QSA]
 
R.I.P Millercent
Loyal Member
Joined
Nov 6, 2012
Messages
2,230
Reaction score
314
Did you restart your web server after you made changes to the htaccess?

Also don't know if this helps but, here is my old version before I started to use IIS

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ /$1.php [L,QSA]

Yes. Purged site cache and all. And doesn't work either. I think I'll have to generate each page rule on it's own.
 
Joined
Jun 8, 2007
Messages
1,985
Reaction score
490
I don't know what you want to happen if /foo exists as a file, or if both qux.html and qux.php exist, so I decided to make a script that says,

If the request_filename does not exist as-is, but does exist with '.php' appended to it, than redirect to the request_filename with '.php' appended to it.
If the request_filename does not exist as-is, but does exist with '.html' appended to it, then redirect to the request_filename with '.html' appended to it.

Since the .php rewrite rules happen first, then it redirects to the .php one first if both .html and .php files exist. However, if a file exists called baz, and a file called baz.html, and a file called baz.php, then the script goes to baz.
Code:
RewriteEngine on
RewriteBase /ht-test


RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php


RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html

So assuming a directory structure like,
Code:
/
 - ht-test/
 - - .htaccess
 - - foo.html
 - - bar.php
 - - baz
 - - baz.html
 - - baz.php
 - - qux.html
 - - qux.php
 - - test/
 - - - foo.html
 - - - bar.html
 - - - bar.php.php

Here are some example request situations:
Code:
/ht-test/foo        -> /ht-test/foo.html
/ht-test/bar        -> /ht-test/bar.php
/ht-test/baz        -> /ht-test/baz
/h../qux            -> /h../qux.php
/h../qux.html       -> /h../qux.html
/h../qux.php        -> /h../qux.php
/h../test/bar       -> /h../test/bar.html
/h../test/bar.php   -> /h../test/bar.php.php

Now, if we created the file, /ht-test/test/bar.php, the last two redirects would change like so:
Code:
/h../test/bar       -> /h../test/bar.php
/h../test/bar.php   -> /h../test/bar.php

I hope that's your intended behavior.

If this script doesn't work for you, then that's weird.. Make sure you have RewriteBase defined explicitly. By looking at the other answers and your original code, it kind of looks like you would've had it working by now.. Maybe there's a setting in httpd.conf you need to change to get htaccess to work properly. Are you using Apache 2.2+ or are you using a version less than 2.2?
 
Last edited:
Back
Top