1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Built-in URL Rewriting for .NET

Discussion in 'C#' started by dgxshiny, May 13, 2008.

  1. #1
    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
     
    dgxshiny, May 13, 2008 IP
  2. itcn

    itcn Well-Known Member

    Messages:
    795
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    118
    #2
    It's actually pretty easy to add your own URL rewrite to the global.asax file, I use it here http://forum.itcn.com
     
    itcn, May 15, 2008 IP
  3. mnn888

    mnn888 Member

    Messages:
    92
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #3
    when is it coming out? i can't wait...
     
    mnn888, May 15, 2008 IP
  4. fhirzall

    fhirzall Guest

    Messages:
    124
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
  5. MatthewDP

    MatthewDP Member

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #5
    Awesome, about time...
     
    MatthewDP, May 26, 2008 IP
  6. homey

    homey Peon

    Messages:
    103
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    About time is right. insane that it took this long.

    Only prob is that not many hosting companies will have the newest version running.
     
    homey, May 27, 2008 IP
  7. itcn

    itcn Well-Known Member

    Messages:
    795
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    118
    #7
    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, May 27, 2008 IP
  8. homey

    homey Peon

    Messages:
    103
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #8
    itcn thanks for the help and nice code. Excellent for dealing with individual pages.
    :cool:
    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.
     
    homey, May 28, 2008 IP
  9. itcn

    itcn Well-Known Member

    Messages:
    795
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    118
    #9
    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.
     
    itcn, May 28, 2008 IP
  10. dgxshiny

    dgxshiny Greenhorn

    Messages:
    65
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    23
    #10
    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.
     
    dgxshiny, May 28, 2008 IP
  11. dgxshiny

    dgxshiny Greenhorn

    Messages:
    65
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    23
    #11
    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
     
    dgxshiny, May 28, 2008 IP
  12. clobberx

    clobberx Active Member

    Messages:
    73
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    61
    #12
    How to do the same in 2.0
     
    clobberx, Jun 13, 2008 IP
  13. centsi

    centsi Active Member

    Messages:
    83
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    56
    #13
    Can it be done in 2.0?
     
    centsi, Jun 17, 2008 IP
  14. itcn

    itcn Well-Known Member

    Messages:
    795
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    118
    #14
    Read my post above, I provide the code to put in your global.asax file for URL rewrites in .NET 2.0
     
    itcn, Jun 17, 2008 IP
  15. Social.Network

    Social.Network Member

    Messages:
    517
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    35
    #15
    I use an ISAPI rewrite engine. I have used it with last two versions of IIS with great success. Check out Helicon.
     
    Social.Network, Jun 27, 2008 IP