Rob's Ramblings

Sunday 6 May 2012

modrewrite and all that jazz

As I discussed previously, I've been working on Tiddler's website. Some past reading about Best Practice suggests hiding technology from the reader, and disassociating the URL from the structure of the website. i.e., don't commit yourself to exposing /cgi-bin/ or foo.php or whatever, since times change, and you might want to re-write things later. Being stuck with incoming links pointing at such places an be a pain. It's not a major site, but I thought I'd try to be good and see how it goes. So, URLs will be simple categories - /foo/ /bar/ etc. The obvious answer is to create these as folders, with a default index file in each, however I didn't want to have to maintain lots of separate files, but keep everything together in the one application, one template, and just call in the different page contents as required. So, behind the scenes, we need a simple handler, index.php in this case, which accepts a "pagename" parameter. e.g. /index.php?p=gallery but how to turn the one into the other? here's the .htaccess

RewriteEngine on


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^new/([a-zA-Z0-9]+?)/$ new/index.php?p=$1
/new/ being the folder I'm developing in. Any accesses to /new/foo/ will be translated into /new/index.php?p=foo but that's never exposed to the visitor - it could just as easily be /cgi-bin/stuff.pl?foo I'm quite pleased, it's simpler, and makes for shorter and less cluttered URLs, which can only help with search engine rankings.

Labels: ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]



<< Home