Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help with mod_rewrite: removing .php extension

Posted on 2008-06-26
6
Medium Priority
?
1,053 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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 response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

963 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