IIS URL Rewrite ASP

前端 未结 2 600
北恋
北恋 2020-12-06 23:20

I do my best to scan the forum for help to make a web.config to do a Rewrite of this kind of url

domain.com/default.asp?id=3&language=2

2条回答
  •  长情又很酷
    2020-12-07 00:12

    I have done this in Classic ASP using custom error pages and this seems to be the best way unless you use some sort of third party component installed on the server.

    To do this, in IIS (or web.config) you need to set up 404 errors to go to a specific custom error Classic ASP page (eg. 404.asp).

    In this custom error page you first need to check to see if the URL is valid. If it is you can Server.Transfer to the correct page, return a 200 response code, and parse the URL there to convert the URL to the values needed for the database lookup, etc. If it's not a valid URL then you show a custom error page and return a 404 response code.

    The code to check for a valid URL and to retrieve the URL parameters will vary greatly depending on your URL structure. But to find the URL requested on the custom 404 error page you have to look at the querystring, which will be something like "404;http://domain.com:80/en/service/".

    Here's some sample code that gets the parameters from the requested URL:

    Dim strUrl, intPos, strPath, strRoutes
    strUrl = Request.ServerVariables("QUERY_STRING")
    If Left(strUrl, 4) = "404;" Then
        intPos = InStr(strUrl, "://")
        strPath = Mid(strUrl, InStr(intPos, strUrl, "/") + 1)
        If strPath <> "" Then
            If Right(strPath, 1) = "/" Then strPath = Left(strPath, Len(strPath) - 1)
        End If
        strRoutes = Split(strPath, "/")
    
        'Here you can check what parameters were passed in the url
        'eg. strRoutes(0) will be "en", and strRoutes(1) will be "service"
    
    End If
    

    And here's how you can setup custom error pages in the web.config (rather than in IIS):

    
    
        
            
                
                
            
        
    
    

提交回复
热议问题