Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Help with mod_rewrite: removing .php extension

Posted on 2008-06-26
Medium Priority
Last Modified: 2012-06-27
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 (newsite.farrapc.com).

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 http://newsite.farrapc.com/applications/blasting/sizing-recommendations, it should display http://newsite.farrapc.com/applications/blasting/sizing-recommendations.php, 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 http://newsite.farrapc.com/applications/blasting/index, which should have displayed http://newsite.farrapc.com/applications/blasting/index.php.  Still got the 404 error.

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

BTW, the site is password protected, so you can't view it yourself.
Question by:light_bulb
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2

Expert Comment

ID: 21882082
You do not allow using "/" in uri - try ([a-zA-Z0-9\/]+)

Author Comment

ID: 21907762
Ok, I tried that, but it's still not working.

After modifying the regex as you suggest, I pointed my browser to http://newsite.farrapc.com/applications/blasting/benefits, but I got a 404 page which said "The requested URL /www/vhosts/f/farrapc.com/htdocs/newsite/benefits.php was not found on this server."

Not sure what's going on here....

Author Comment

ID: 21907808
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.
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.


Expert Comment

ID: 21907874
Does server have this file /www/vhosts/f/farrapc.com/htdocs/newsite/benefits.php ?

Server said "The requested URL /www/vhosts/f/farrapc.com/htdocs/newsite/benefits.php was not found on this server."
Is it true?

Author Comment

ID: 21907921
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?


Accepted Solution

light_bulb earned 0 total points
ID: 21910220
Ok, here's what I came up with that works.  Found this at: http://www.webmasterworld.com/apache/3371997.htm

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]

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As Wikipedia explains 'robots.txt' as -- the robot exclusion standard, also known as the Robots Exclusion Protocol or robots.txt protocol, is a convention to prevent cooperating web spiders and other web robots from accessing all or part of a websit…
If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secure…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question