Help with mod_rewrite: removing .php extension

I'm trying to be able to remove the .php extension from my urls using mod_rewrite in a .htaccess file.   The .htaccess file is in the top-level directory of the site (

Here's what I have:

CheckSpelling Off
ErrorDocument 404 /404.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^/?([a-zA-Z0-9]+)$ $1.php [L]

Problem is, that this isn't working.  When I type in, it should display, but I get a 404 not found error page instead.  I wondered if the regex was catching the dash in sizing-recommendations, so I tried, which should have displayed  Still got the 404 error.

What am I doing wrong here?  I just got the above code from

BTW, the site is password protected, so you can't view it yourself.
Who is Participating?
light_bulbConnect With a Mentor Author Commented:
Ok, here's what I came up with that works.  Found this at:

RewriteEngine on
## Internally rewrite extensionless file requests to .php files ##
# If the requested URI does not contain a period in the final path-part
RewriteCond %{REQUEST_URI} !(\.[^./]+)$
# and if it does not exist as a directory
RewriteCond %{REQUEST_FILENAME} !-d
# and if it does not exist as a file
RewriteCond %{REQUEST_FILENAME} !-f
# then add .html to get the actual filename
RewriteRule (.*) /$1.php [L]
You do not allow using "/" in uri - try ([a-zA-Z0-9\/]+)
light_bulbAuthor Commented:
Ok, I tried that, but it's still not working.

After modifying the regex as you suggest, I pointed my browser to, but I got a 404 page which said "The requested URL /www/vhosts/f/ was not found on this server."

Not sure what's going on here....
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

light_bulbAuthor Commented:
Or if I try  ^/+([a-zA-Z0-9]+)$ $1.php [L]

I get this message on the 404 page:

The requested URL /applications/blasting/benefits was not found on this server.
Does server have this file /www/vhosts/f/ ?

Server said "The requested URL /www/vhosts/f/ was not found on this server."
Is it true?
light_bulbAuthor Commented:
Oh, man.  I made a mistake.  There isn't a newsite/benefits.php file.  It's at newsite/applications/blasting/benefits.php

So the script isn't putting in the "applications/blasting" part that I need.  How can I accomplish that?

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.