What is wrong with this proposed Router for Vanity-URLs solution?
I recently posted a question about creating vanity URLs on my site and while I got many great answers, I am curious to see what was wrong with my original idea:
Here is the problem: I currently have ugly URLs that look like this: http://www.hikingsanfrancisco.com/hiker_community/hiker_public_profile.php?community_member_id=2
What I want is something elegant like this: http://www.hikingsanfrancisco.com/alex-genadinik
What my proposed solution is: Can开发者_Python百科 I just store "alex-genadinik" in the database (instead of MVC controllers) and fetch the vanity URL when look up the person. And then I could redirect to the elegant-looking domain?
Or will the redirection add a level of technical-awkwardness and inflexibility? Maybe I wouldn't have to redirect at all. I mean I'd be looking things up in the db instead of the server. I think thats the only difference. Thoughts? :)
There's nothing wrong with your proposed solution. In fact WordPress can be configured to do exactly this.
I would implement it as follows (similar to WordPress):
- Have an .htaccess file catch all non-existant files that are requested, and rewrite to your router.php
- Have your router.php check the for this non-existant path in your db, against a list of pre-calculated vanity names.
- If it exists, display the relevant information, if not return a set of 404 headers to show that neither the file or the vanity name exist.
URL Rewriting Guide
Drupal actually does something similar, all URL aliases are stored in the database. You can do it however you want! I would think about the different types of "thing" you will have URLs for and how to distinguish them, how you are going to do the lookup when a user requests a URL, how you will do the inverse lookup to generate links from entities, and how to handle parameters (i.e. /vanity-url?sortby=blah) if you will ever need them.
精彩评论