Solved

Help with mod_rewrite: removing .php extension

Posted on 2008-06-26
6
1,044 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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
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 is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

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…
If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

895 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

15 Experts available now in Live!

Get 1:1 Help Now