Question

I would like to know how to handle a problem which I guess is somewhat generic. But i am just not sure how it is supposed to be done. So hopefully people can find the way around what may be ill formed thoughts and bad terminology.

I did a tinyurl-like service just as an example, nothing which should be actually used. It's written in php and uses mysql. All well it's only one problem I couldn't really figure out, and I feel like I've run into this before.

The problem is when expanding the generated url (for simplicity let's assume that the base url is http://a.com and that the links are ordered by an auto-incremental index so i just give the database index when giving a tinified url.

For tinyurl a link would be for example: http://tinyurl/abcd

I however used a parameter and end up doing something like http://q.com?u=0

I guess that if I want to have a format like the first one I could set up .httaccess in my apache server to use a custom 404 page so when going to: http://a.com/0 I would actually get a 404, redirected to my own 404.php and in that page handle the database lookup and redirect to the real url.

My problem is that I just have this gut feeling that using 404 as a "feature" is a bit dirty. And my question is if this would be the general way to do it, or if there are some other ways which I just don't know about.

Sorry for the rant and thanks for any information!

Was it helpful?

Solution

You should have a look at mod_rewrite! In your .htaccess File you could do something like this: RewriteEngine On RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?u=$1

That might not be the best rule, but there are plenty tutorials out there!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top