Solved

Help with mod_rewrite: removing .php extension

Posted on 2008-06-26
6
1,043 Views
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.
0
Comment
Question by:light_bulb
  • 4
  • 2
6 Comments
 
LVL 1

Expert Comment

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

Author Comment

by:light_bulb
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....
0
 
LVL 3

Author Comment

by:light_bulb
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.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Expert Comment

by:webjema
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?
0
 
LVL 3

Author Comment

by:light_bulb
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?

Thanks
0
 
LVL 3

Accepted Solution

by:
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]
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now