ASP.NET 3.5 SP1 is going to have built in support for URL Rewriting ... finally. from: http://weblogs.asp.net/scottgu/arch...nd-net-framework-3-5-service-pack-1-beta.aspx
It's actually pretty easy to add your own URL rewrite to the global.asax file, I use it here http://forum.itcn.com
It's already out. http://weblogs.asp.net/scottgu/arch...nd-net-framework-3-5-service-pack-1-beta.aspx
About time is right. insane that it took this long. Only prob is that not many hosting companies will have the newest version running.
As I said, I've been doing URL rewriting with .NET for years using the global.asax file. The following .NET 2.0 code will change pagenames from this: /pagename.aspx?id=101 to this: /mypage/101/whatever-file-name-you-want.aspx Sub Application_BeginRequest(ByVal sender as Object, ByVal e as EventArgs) Try Dim httpContext As System.Web.HttpContext = httpContext.Current() Dim currentURL As String = Request.ServerVariables("PATH_INFO") Dim finalURL As String finalURL = rewriteUrl(currentURL) httpContext.RewritePath(finalURL) Catch ex As Exception Response.Write("Error in Global.asax :" & ex.Message + Constants.vbLf + Constants.vbLf + ex.StackTrace.ToString() & Constants.vbLf + Constants.vbLf) End Try End Sub Public Function rewriteUrl(byVal thisURL As String) Dim currentFileName As String Dim currentFileType As String Dim currentFileID As String Dim returnURL As String Dim rewrite_regex As Regex = New Regex("^.*/(?<fileType>[^/]+)/(?<identifier>\d+)/(?<fileName>[^/]+)(\?.*)?", RegexOptions.IgnoreCase) Dim match_rewrite As Match = rewrite_regex.Match(thisURL) currentFileID = match_rewrite.Groups("identifier").Value currentFileName = match_rewrite.Groups("fileName").Value.toString() currentFileType = match_rewrite.Groups("fileType").Value.toString() If currentFileID <> "" Then If currentFileType = "mypage" Then returnURL = "/pagename.aspx?id=" & currentFileID Else '### NO MATCH, JUST PASS TO REQUESTED URL returnURL = thisURL End If Else returnURL = thisURL End If Return returnURL End Function Code (markup):
itcn thanks for the help and nice code. Excellent for dealing with individual pages. What I mainly want is redirecting at the domain level. I think this is not possible in 2.0 and needs to be done in IIS. for example user types in, or clicks link: http://www.sitename.com and gets permanently 301'd to : http://sitename.com the whole 'google cannonicalization' issue so google will be forced to see only one site. I hate dealng with bloody hosting companies it would nice to be able to do this on my own without involving them.
Ah yes, for 301s -- but it still works the same way. I still use the PATH_INFO redirect method, and create 2 sites. Then just put the redirect code in the global.asax file of the site you want to redirect to the other one. The regex is a bit different, though.
You can getaway with doing something like string serverInfo = Request.ServerVariables["SERVER_NAME"].ToString(); string cUrl = Request.FilePath; //forward all traffic to the www. domain unless we are testing internally if (!serverInfo.Contains("www.domain.com") && !serverInfo.Contains("192.168")) { Response.Status = "301 Moved Permanently"; Response.AddHeader("Location", "http://www.yourdomain.com" + cUrl); Response.End(); } Code (markup): This will 301 any request not to www.domain.com. I use this on the masterpage of some of my smaller sites.
also, you made need to handle query strings as well string cParams = Request.QueryString.ToString(); if (cParams != "") cParams = "?" + cParams; Code (markup): and you can append that to your redirect
I use an ISAPI rewrite engine. I have used it with last two versions of IIS with great success. Check out Helicon.