Jump to content

Trim The ".php" With .htaccess And Mod_rewrite


Recommended Posts

First of all, I've only been a customer for a few days, and WOW! TCH is amazing! I'm truly in love. Now on to the problem :)

 

I have a site that is driven by a bunch of php scripts. I use the extra path info directive to avoid having ?s and &s in the URL. I also prefer to strip the ".php" from my URLs, so that I have something like this:

http://www.isaacschlueter.com/blog/2004/05

to show all the posts in the "blog" category, from May, 2004.

 

Of course, the actual file is "blog.php". I was doing this on my previous webhost by putting the following in my .htaccess folder. (It's a little ugly, but it's the only way that I could get it to work.)

>RewriteEngine On
# If it's just /blah, change it to /blah.php
RewriteRule (^[^\./]+)$ $1\.php
# If it's /blah/1/2/3, change to /blah.php/1/2/3
RewriteRule (^[^\./]+)/([^\.]*)(.html?)?$  $1\.php/$2$3

# Cleanup Unwanted the RR Tracks, for cases when it's a valid directory
# If it's /validdirectory.php/1/2/3, change to /validdirectory/1/2/3
RewriteRule ^(xmlsrv)(\.php)/(.*) $1/$3
RewriteRule ^(htsrv)(\.php)/(.*) $1/$3
RewriteRule ^(admin)(\.php)/(.*) $1/$3
RewriteRule ^(skins)(\.php)/(.*) $1/$3
RewriteRule ^(ari)(\.php)/(.*) $1/$3
RewriteRule ^(httest)(\.php)/(.*) $1/$3

When I do this, it works fine for the URLs. And, for some reason, I can go to http://www.isaacschlueter.com/admin/whatever.php without any problem. But, http://www.isaacschlueter.com/htsrv/login.php gives an Error 500: Internal Server Error.

 

I'm sure that it's some sort of problem with the RewriteRule stuff, because when I remove them, then everything works (but I have to put .php on my urls, of course.) For the time being, I've removed them all, but if you'd like to see what happens, just post here, and I'll upload the .htaccess again.

 

Any ideas? Anyone know of a cleaner way to do what I'm trying to do?

 

If I manage to figure it out, I'll post here to help any lost souls in this predicament in the future :)

 

Thanks :)

Link to comment
Share on other sites

:)

 

I had accidentally dropped a .htaccess file in my htsrv folder, and it was caught in an infinite rewrite! So, everything's working now.

 

Anyhow, this is a much cleaner way to do it.

>RewriteEngine On

# Extra Super Duper Clean URLs
# list all of the valid directory names separated by pipes.
RewriteCond %{REQUEST_URI} !^/(folder1|folder2|folder3)(.*)$
RewriteRule (^[^\./]+)$ $1\.php

RewriteCond %{REQUEST_URI} !^/(folder1|folder2|folder3)(.*)$
RewriteRule (^[^\./]+)/([^\.]*)(.html?)?$  $1\.php/$2$3

I'm looking into using the -d flag in the RewriteCond to check if it's a valid directory, since hard-coding the dir names is a bit ugly. Of course, it's still not nearly as ugly as rewriting, then re-rewriting back to the way that it was! And, in a way, it's more secure, since I specifically grant access this way to certain dirs, and all others get a 404 for the "missing" php file.

Link to comment
Share on other sites

Another way to do this, instead of allowing access to the folders, is to only rewrite certain files...

>RewriteEngine On

# Extra Super Duper Clean URLs
# list all of the file names to rewrite separated by pipes.
RewriteCond %{REQUEST_URI} ^/(file1|file2|file3)(.*)$
RewriteRule (^[^\./]+)$ $1\.php

RewriteCond %{REQUEST_URI} ^/(file1|file2|file3)(.*)$
RewriteRule (^[^\./]+)/([^\.]*)(.html?)?$  $1\.php/$2$3

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...