Blog Entry - 23th May 2009 - Programming - Web Server Configuration

URL Voodoo 2 - PHP

The second approach to URL re-writing I have encountered is do use PHP instead.

Suppose we want http:localhost/a/blog/12/ to be treated as if it were http:localhost/a/blog.php?id=12.

We can do this, but with limitations. Here is the process:-

Step 1

We want to force APACHE to treat blog in the URL as a PHP file.

Do do this, we insert an .htaccess in the /a/ directory, as follows:-

<FilesMatch "\bblog\b"> 
  ForceType application/x-httpd-php

This matches blog where it appears as a whole word (\b word boundaries), and it then forces the computer to treat blog as a php file.

Step 2

Create a file called blog in the /a/ directory.

This file should have no extension, and should contain the PHP.

This is perhaps the weakness of this method, we have to do away with php extensions.

The content of blog will be:-

function Debug_Dump($obj /*: Any*/) /*: String*/
	$a = ob_get_contents();
	return "<pre class='dump'>" . htmlspecialchars($a, ENT_QUOTES) . "</pre>";

$data = preg_split("/\//",$_SERVER['PATH_INFO'], -1, PREG_SPLIT_NO_EMPTY);
echo Debug_Dump($data);

PATH_INFO will contain everything after blog, i.e. /12/.

To access its contents we need to split it on / boundaries, and ignore empty chunks. preg_split is best for this.

I need to play around with this further.


No comments made on this entry.

Leave a comment ...

{{PREVIEW}} Comments stopped temporarily due to attack from comment spammers.

Other Recent Entries in Web Server Configuration