Create clean urls and managing duplications
Im using asp.net mvc c#. Based on user input I want to sa开发者_如何学Pythonve the text they enter in the db and create a url using this text. there will be a unique id associated with this text too.
What should I factor when creating "clean" urls?
and also how should I avoid repetition - i.e every url should be unique, so if 2 people type in: "Hey, Check My cool URL!!"
I'd probably want something like http://www.mysite.com/links/hey-check-my-cool-url/
How can i ensure they will always be unique and what should i do if the same description is entered twice?
Most people end up with some variation of loannis karadimas solution. However, two schemes that that I tend to use are:
http:///www.mysite.com/links/xxx/hey-check-my-cool-url
This is because it indicates that the xxx digits aren't part of the title of the link (depending on what you are doing).
http:///www.mysite.com/links/hey-check-my-cool-url-xxx
http:///www.mysite.com/links/hey-check-my-cool-url/xxx
Basically sticking the xxx digits at the end, because for SEO purposes (if you care) it is more likely that words closer to the root of your URL has higher predence for page rank. Of course take that with a grain of salt, like most other SEO tips!
PS, I don't know much about ASP at all.
You could prepend a small (random) sequence or force the user to enter a different phrase, depending on your requirements (I would prefer the first):
http:///www.mysite.com/links/xxx-hey-check-my-cool-url/
I would factor in only letters and digits, removing everything else by using a simple regular expression as a whitelist approach, i.e.:
[^\w\d]{1,}
This expression should match everything that's not an alphanumeric.
精彩评论