views:

22

answers:

1

I'm trying to alter the way we currently provide a gateway page system within our CMS. What i mean by gateway page is mapping a non-existent URL to a page through a rewrite rule in the .htaccess, e.g.

RewriteRule ^foobar$ page.php?mode=bar&method=foo&id=1

This allows people to create short links to CMS pages for magazine adverts etc. The problem with this method is that it relies on access to the .htaccess. I would prefer a method that sits at code level but it occurs to me that - without a rewrite rule - a 404 error will be called. Is there any way to prevent this or work around this?

+4  A: 

You can use a rewritemap within your htaccess. What this does is references an external file/script, passing it the incoming uri and getting back the rewritten uri.

In your .htaccess

RewriteMap shorts prg:/path/to/map.php

In your php file map.php

#!/path/to/php
$keyboard = fopen("php://stdin","r"); // get data from stdin
while (1) {
    $line = trim(fgets($keyboard));
    // fetch rewrite for line and echo out
}

The php file is passed the short url and returns - based on your logic - the full url

adam
I do like this option, i assume though that it still falls down where any .htaccess rule falls down - incorrect syntax = 500 error?
seengee
I'm not sure, you'll have to run a test. You could try returning null, see how the rule handles that. Sorry, I'm a bit rusty!
adam