Hiding PHP with pretty URLs

1 September 2009

For my websites, I insist the URLs look pretty and the PHP extensions are hidden. For my next project (a re-launch of my flagship band) I’m aiming to do this differently :P

Before I would use Apache2 with mod_rewrite using a lengthy variation of this script:

RewriteCond %{THE_REQUEST} ^GET (.*).php HTTP
RewriteRule (.*).php$ $1 [R=301]
...

At the end I would hide server side files that I wouldn’t want accessible from the outside, such as templates and “spit out” scripts. The actual script I used would search for a PHP file with the requested name in it, and if it existed would spit it out else throwing up a 404.

I’ve thought of doing things differently this time around. I’m gonna do this – just use extensionless files. So for example, /httpdocs/home is now a PHP file that Apache will spit out to the user as HTML parsed from PHP. I just need to tell Apache this. The above code can be changed to this:

DefaultType application/x-httpd-php
...

I’d still need to hide some files but this approach means I don’t have to have as many files to catch the queries. I’ll have the content in the extensionless file with an include to the main script. That catches the content and brings in the templates.

On a different note

When I next get time I’m going to convert a Mac Mini into an Ubuntu server. I’m not sure if it’s PPC yet so it could be interesting. Currently I’m testing everything on the same machine I’m writing the stuff on, an old P4 Presario machine with Ubuntu Studio Jaunty on it, having no problems.

With a dedicated machine I could use another PC in the house, say a Windows one, SSH in and do my thing. I’ll be keeping things consistent. An actual testing server has many more advantages.

I’m busy with my band at the minute. Should really blog more about gigs and bands. Ah well, maybe soon ;-)

Calm & Madness