Solved

Help with mod_rewrite: removing .php extension

Posted on 2008-06-26
6
1,047 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

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…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

679 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