Creating User-Friendly URLs With IIS
Something I've seen and always wanted to emulate are URLs like this:
http://stackoverflow.com/questions/1491939/can-e4x-have-an-array-passed-to-it
As you can tell just from looking at this URL - it's a question on StackOverflow.com about whether you can pass array to E4X. And you can tell all that simply by looking at the URL!
Not only is this good for the user (perhaps) but it's good for Google and SEO of a site in general.
But how do you do it? I'd always assumed (naively) that you'd have to:
- Store the URL-safe version of the question in the database.
- Ensure this URL-ready version of the title was unique.
Turns out neither of those are true. The only part of the URL the backend code cares about is the question ID (1491939).
How To Do It:
I'll cover IIS as that's what I've been using just recently. First open IIS manager app and find the URL Rewrite plugin for your website:
In there choose Add Rule and then "User-friendly URL":
In the next dialog you enter the URL of the ASPX page as the server expects it and then the Rule works out the user-friendly for it, as you can see below, using the Stackoverflow example:
You also get the option to have IIS rewrite out-going HTML to make sure all internal links point to this new friendly format. I left this un-ticked though and just made sure I stuck to the new convention when building my pages.
As you can see it uses Regular Expression to re-write the URLs. Notice it's sending the title part of the URL to the server as a parameter. It doesn't need to though and this parameter isn't referred to by the code.
That's how simple it is though. I can't believe it didn't dawn on me that it would be this straight-forward. Doh!
Here are some example of it in use with a new site I'm working on (for myself).
http://www.grouplets.co.uk/region/247356/the_peak_district/
http://www.grouplets.co.uk/property/163524/hurdlow_grange/
Is it just me that gets a warm fuzzy feeling inside from being able to do stuff like this. Must be a geek thing.
Isn't part of creating User Friendly URLs that they can be easily remembered by an end user? Having a number in them defeats the object slightly. Wouldn't something like /region/the_peak_district be better?
Not sure how easy it would be to create thou. Just my thoughts.
Reply
You're probably right Jon. Notice the getout word in brackets from above:
"Not only is this good for the user (perhaps) "
I say perhaps as I'm not actually sure how URLs are seen or used by the average web user. Do they pay much attention? I don't know. Do they ever remember them or type them in - or do most people just use Google to find *everything*. I know my dear wife uses google (her browser homepage) to search for www.boots.com and then clicks the first result to go there. I told her about the address bar once but it was lost on her.
What the site could do is have a way to intercept URLs like:
//grouplets.co.uk/peakdistrict
And 301 redirect them to the right URL. One for the list.
Reply
Show the rest of this thread
Keep up the IIS and .aspx posts Jake! Unfortunately we have just decided to jump off the domino ship and are boarding the Exchange/Sharepoint/.net ship now!
Reply
It's probably unavoidable that there'll be more ASP.NET talk coming up on here.
I've always tended to talk about whatever it is I'm mainly doing at the time and - if things go to "plan" - ASP.NET will be playing a large part in my workload for the coming year(s).
Reply
Am Sorry Lence :)
Reply
You can achieve the same thing by using the Routing functionality included from .NET 3.5SP1 onwards. Although it was originally intended for use with MVC it works nicely with traditional webforms apps too. The advantage with that approach is no IIS configuration changes are needed, i.e. handy for shared webhosts.
Reply
Thanks Jake, interesting article!
Reply
On (somewhat) of a related note, there are also services such as:
http://tinyurl.com/
Reply
You're right to question what normal users see/use in relation to urls.
Pretty much all my users do not know what the address bar is. If I ask them to type an address in (specifically stating where, I might add), the response is usually "OK, now I've got a long list of them, which do I open" (can you see, what they did :-) )
The other day I watched someone open up the browser, which happened to default to bing, search for google and then search for the item they wanted......
I think as long as your main site links are memorable (so, the homepage url and pretty much then the categories or channels) then anything else is just tidywork.
Reply