...

Problem removing .php extension with mod_rewrite

johnnyb
11-12-2008, 10:43 PM
Hey all,

I want the URL http://domain.com/myfile to access http://domain.com/myfile.php

I figured the following rewriteRule would do it:
RewriteRule ^([^/]+)/?$ $1.php

In my head it should grab everything up to the slash, with a slash being optional, and get that grabbed stuff with a .php ending, however, I get a 500 error.

So, I tried the following 2 possiblities:


RewriteRule ^([^/]+)$ $1.php # type domain.com/filename into the browser
RewriteRule ^([^/]+)/$ $1.php # type domain.com/filename/ into the browser


Oddly enough, the first of these two options results in a 500 error, and the second works fine.

Does anyone know what I'm doing wrong here?

Thanks.

Fou-Lu
11-13-2008, 07:53 AM
Its greedy greedy. I've never used it for something so simplistic before.
Here's the problem. Damn, I just lost it. Oh well.
Here's how to fix it, be more specific on the characters you allow. The last one works since you're forcing it to end with a / on it. This example states that a / can exist, but may not, so you don't need to write a second rule for it.
Change you're characters with whatever you want to allow (mines just a word character, so a-z0-9 and _)

RewriteRule ^([\w]+)/?$ $1.php

johnnyb
11-13-2008, 08:27 PM
Greedy am I!

The word character is a great idea.

I also made it work like this, (it's a bit more involved, and has a few more features):

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ help/$1 [R=301,L]

# 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 ^([^/]*) help/$1.php [L]
## Externally redirect clients directly requesting .php page URIs to extensionless URIs


# If client request header contains php file extension
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+\.)+php\ HTTP
# externally redirect to extensionless URI
RewriteRule ^(.+)\.php$ help/$1 [R=301,L]



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum