How can I serve unique URLs based on a file ID?
February 10, 2010 7:25 PM   Subscribe

I'm about to ask a potentially goofy question about serving unique URLs. This is something I should know, but- How do I serve a unique URL based on a common PHP script where the URL is distinguished by a unique ID?

I have a small app where User A can upload File A and File B and on and on. In theory there will be a few hundred users all uploading files.

All users have a unique ID. All files have a unique ID. I have a common set of PHP scripts to pull data from the DB and display information: File name, file URL, comments, description etc. Let's say it's called "file_display.php".

My problem is this. Let's say User C clicks on a file with ID 123. Instead of sending that user to http://mydomain.com/file_display.php and using the ID to pull from the DB, I'd like to be ale to send them to http://www.mydomain.com/123 and display the data that way so that users can bookmark and share links to files easily. Alternately, something like http://www.mydomain.com/?id=123 is fine.

Basically, I don't know what I'm looking for. I'm sure what the "technique" is. Help?
posted by GilloD to Technology (7 answers total)
 
Best answer: mod_rewrite
posted by elle.jeezy at 7:27 PM on February 10, 2010


Yeah, use mod_rewrite to look for numbers or other distinctive strings in the URL, then pass those parts to your script as CGI parameters.

You do know regular expressions, right? 'Cause that helps a lot.
posted by amtho at 7:37 PM on February 10, 2010


Yup, mod_rewrite.
posted by ixohoxi at 7:39 PM on February 10, 2010


Best answer: mod_rewrite is probably the most standard way to handle this. What you do is internally translate URLs from mydomain.com/123 to mydomain.com/file_display.php?id=123. The user is none the wiser and you're one step closer to a RESTful website. One thing that probably makes sense here is to give each unique id a directory scoping. So you'd have mydomain.com/files/123. And perhaps mydomain.com/users/123.

Bear in mind however, that mod_rewrite is an exercise in regex. Hopefully as a PHP user you're comfortable with this.
posted by pwnguin at 7:47 PM on February 10, 2010


Response by poster: Well! Always learning, this guy. Thanks, dudes. I love starting on a project and you're totally sure you know how it's going to go down and then all of a sudden it's like. A brand new experience.

Anyway! Thanks so much!
posted by GilloD at 7:56 PM on February 10, 2010


Mod_rewrite works fine, but another way (which I happen to like better) is to use the fact that apache will happily accept path components after your script's url and give them to you in $PATH_INFO. So, say you have a php script at http://www.example.com/file (.php), you can retrieve http://www.example.com/file/123/456 and your script will be invoked with $PATH_INFO equal to "/123/456". You then split that on slashes, and use the parts to retrieve the file, just like before.
posted by hattifattener at 11:34 PM on February 10, 2010


I spent a good many hours just last week tinkering with mod-rewrite for the first time - it can be frustrating. Not that AskMe isn't awesome, but for the nitty-gritty details, I found searching the stack overflow archives very helpful. Good luck!
posted by oulipian at 5:57 AM on February 11, 2010


« Older Help playing audio on Japanese BDs?   |   Cellphone to laptop? Newer »
This thread is closed to new comments.