views:

645

answers:

5

So, I know there's a lot of this subject here and over the Internet. But most articles/questions refers to "static" url rewriting, like:

www.site.com/products.aspx?category=Books

So they rewrite it to

www.site.com/Products/Books

That's ok but I need something else.

The site is like a CMS, it has different types of content.

Nowadays to read the article titled "How StackOverflow helps you in your development" you need to go to an URL like the following.

www.site.com/viewContent.aspx?Id=1234

What I want to achive is:

www.site.com/Content/Articles/how-to-stackoverflow-helps-you-in-your-development

So as I understand, I need to involve ASP .NET in that, because first I need to retrieve the article (an its title of course) and then rewrite the URL.

But I'm wondering how the hell ASP .NET will know how to get that article if I go to that URL, it doesn't include the id anywhere... So maybe I could accept something like

www.site.com/Content/Articles/1234/how-to-stackoverflow-helps-you-in-your-development

I'm kind of lost here really.. I've never done any URL Rewriting at all and I've googled a lot and I cannot find a way to do what I want. Maybe what I want is not called url rewriting??? I don't know...

The site is running under Windows 2003 Server, IIS6, ASP .NET 3.5 SP1 And of course, I need a free solution, cannot spend 100usd on the ISAPI mod (besides I don't know if that is going to do what I need).

Thanks to all and sorry if this is a duplicated question, but I couldn't find it.

EDIT: I don't need to support non-ASP.NET files (jpgs, gifs, etc) don't need to be rewritten. I just need to rewrite the viewContent page to include the content title into the URL.

A: 

ASP.net MVC is your friend for that

stackoverflow use MVC

Fredou
The site has been done intially in plain ASP, then migrated to ASP .NET 1.1, then 2.0, then 3.5. It's a big site, I cannot re-make the whole site with MVC just to include the SEO friendly urls...
emzero
you can mix mvc and webform
Fredou
Really?? How? I've read/seen several ASP .NET MVC tutorials and they all start from "new project -> ASP .NET MVC project".... So how can I mix it?
emzero
after you created a new project, right click on the project and add new item then select asp.net webform
Fredou
That would be similar to migrate the project, I still need to create a new empty MVC project and add every webform? I don't think that's a good approach, but thanks anyway.
emzero
I didn't try the other way around but it should be easy, just look at the difference between a webform and mvc web.config from that point on, you just need to create the folder structure / reference mvc dll
Fredou
-1: It is not a viable solution to change a big site to a use MVC, just because you want the SEO-URLs
Dofs
+2  A: 

This one is free, I have used it and it works pretty well: http://www.codeplex.com/IIRF

The nice thing is that it will handle url's without an extension (i.e. .aspx, .html, etc.)

John JJ Curtis
+4  A: 

You can use the new Routing that comes with ASP.Net 3.5 sp1 to have clean URLs.

This can even be done in web forms and not just MVC. ( I have done it myself). See here and here for exanples of how to set it up.

You can throw the Id of the article and the title in the URL and make the Id the real parameter that gets used to search for the article. That is what SO does. Try removing the question for the URL and it will take you to the same place.

Even if you don't use the Id you can pass the title of the article "how-to-stackoverflow-helps-you-in-your-development" to your DB and retreive the article based on the title.

With regards to IIS 6 it is a little trickier since IIS 6 by default can't handle extension-less URL's.

There are a few work arounds:

  1. Use the wildcard mapping in IIS to map all requests to Asp.Net
  2. Put Default.aspx at the end of your urls

See this post for other possible solutions.

Although the first solution may have performance issues if all content in your site goes through Asp.Net (even images, css, .html ...) in a small site it shouldn't matter. I have used this approach and there wasn't any major performance issues. I think it is the simplest solution. Here is the website I built with it

I hope this helps.

orandov
I think I will go for your approach. So I will be using just .NET to rewriting right? I don't need any IIS addon or filter right?Anyway I will include the id in the url because the title is not a unique column so there could be more than 1 content object with the same title.Thank you, I'll try it and let you know!vote up for you =)
emzero
I added the IIS 6 work arounds.
orandov
Thanks, but I'm not having any luck with it. I've read all of your links but I cannot make it work. I don't fully understand how it works really. I've followed the steps on the first link you wrote but I cannot make it work. I've set the Route, RouteHandler and everything. But question... do I need to rewrite every link on my site to point to the new "virtual" url? Isn't a way to add some code to the viewContent.aspx page to rewrite the url? But I guess I'd need the other way around when people copy that url... I'm kind of lost really. WHEN do I get the content title and put it in the url???
emzero
Forgot to mention that I don't mind including ".aspx" in the url... I'd prefer that rather than wildcard mapping to avoid performance issues.So a url like www.mysite.com/content.aspx/1234/this-is-the-content-title would be enough to me. But I don't understand where I need to actually get the parameters and "pass" them to the routing class.. I'm lost =(
emzero
Did you download and run Chris Cavanagh's code. You can download it here. http://aspnet35routing.codeplex.com/SourceControl/ListDownloadableCommits.aspxIf you want to only using routing in your site then any existing link in your site would have to be in the form of one of your routes (www.site.com/Content/Articles/how-to-stackoverflow-helps-you-in-your-development).So you would have to fix all of your old links. I have only recently learnt routing recently so maybe someone else has other suggestions.I can't write too much here. You can email me at: olevin at merlenorman dot com.
orandov
I'm still fighting with it. So there's no option to have the 2 links? The old one and the new routed one? Btw, the link you paste is broken...
emzero
A: 

Easiest way is to add a http module to your current webforms project.

http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx

This shows you the basics of it, But it can easily be expanded so that the pages you want to rewite is taken from a database or even built on the fly.

TheAlbear
Yes, I think that's the best way. Again I need to ask if my "old" url will be still available (I mean the viewContent.aspx?Id=1234) and then when they enter through that old url, the page will rewrite it to include the content title. And then I need the viceversa... from the "rewritten" url (which will include the id of course) get the id and then load the article from the db as normal.
emzero
Yes with the rewritter as I use it the old url will still work and display the page. But if you have back links, you will want to add something to check the 'old' URL's and then redirect then to the new SEo friendly, or you run the chance that the google spirders will find the same page content on two diffrent URL's which is very bad.
TheAlbear
+1  A: 

You can achieve this with ASP.NET routing.You can do this with ASP.NET MVC as well as Webforms.No need to do anything with IIS.

Check the below link

http://www.4guysfromrolla.com/articles/051309-1.aspx#postadlink

I did URL routing in my web application within 1 hour with the details from the above link.Its quite simple to learn .They provide sample codes too.It will help you to do it easily

You can retrieve the Id of the content using the title.But title should be unique.You can use ajax to check whether the title is a already existing one when user takes mouse out from the textbox.

Shyju
Thanks, but titles are not unique, and it shouldn't be. What I want is just a way to rewrite the URL, without losing the old URL. As far I understand this is not possible using ASP.NET routing.I mean... I want to have both: www.site.com/Content/Articles/1234/how-to-stackoverflow-helps-you-in-your-development and www.site.com/viewContent.aspx?Id=1234. If that's not possible, I would have to rewrite every link on the site, and people already may have favorited some... so that's not an option. Am I being clear? =PThank you!
emzero