Saturday, February 4, 2017

note to self: custom pretty url path handling/redirect under apache

One popular trick is to make "pretty urls", so you can have something like
http://chat-o-tron.alienbill.com/band/jphonk
instead of
http://chat-o-tron.alienbill.com/viewband.php?name=jphonk

(On my site http://kirk.is/ that's how I get stuff like http://kirk.is/2017/01/01/ working)

So, for reference to my future self, here's what you do:
In an .htaccess file in the webroot for the subdomain do something like

Options -Indexes
RewriteEngine On
RewriteRule ^band\/(.*)$ view.php?info=$1 [L]

And then a sample development view.php might start

<?php
$path = $_GET["info"];
$parts = explode("/",$path);
foreach($parts as $part){
  print "GOT ".$part."<br>";
}
?>

You have options to do more work in the regex rather than in the script things land on, like if you want different levels of pathness to go to different files, but the syntax can be arcane.

No comments:

Post a Comment