2

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

My hope is that this can be

domain.com/en/service

where language=2 is "en" and id=3 is page "Service" (this name exist in a mySQL)

I can only find example that do it vice versa...

Like this

<rewrite>
  <rules>
    <rule name="enquiry" stopProcessing="true">
      <match url="^enquiry$" />
      <action type="Rewrite" url="/page.asp" />
    </rule>
  </rules>
</rewrite>

I would like it to be something like this... I know this isn't correct, but maybe explains my problem.

<rewrite>
  <rules>
    <rule name="enquiry" stopProcessing="true">
     <match url="^default.asp?id=3&language=2$" />
     <action type="Rewrite" url="/en/serice" />
    </rule>
  </rules>
</rewrite>
David
  • 34,223
  • 3
  • 62
  • 80
patrikc
  • 33
  • 1
  • 5

2 Answers2

2

If you want to use regular expressions you could do something like this

<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
     <match url="^([^/]+)/([^/]+)/?$" />
         <conditions>
             <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
             <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
         </conditions>
     <action type="Rewrite" url="default.asp?language={R:1}&amp;id={R:2}" />
</rule>

This would rewrite "domain.com/en/service" as "domain.com/default.asp?language=en&id=Service", or "domain.com/2/3" as "domain.com/default.asp?language=2&id=3"

To change the 2 to en and the 3 to service, along with all the other options though I think you would need a separate rule for each permutation, or have some sort of logic within your asp pages to read your querystring variables and send the corresponding values to your SQL queries. Note also that the parameters in the friendly url appear in the same order and the querystring variables in the rewritten URL, although this shouldn't really be an issue. If someone tries to access the page with the original "unfriendly" url they will find what they are looking for, whichever way round they enter the querystring variables.

Please note, I didn't actually hand code the rule above, I generated it with the URL Rewrite module in IIS manager - it makes life a lot easier

Also note, as discussed with my namesake in the other answer, this only applies to IIS7 and above

John
  • 4,658
  • 2
  • 14
  • 23
1

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):

<?xml version="1.0"?>
<configuration>
    <system.webServer>
        <httpErrors errorMode="Custom" existingResponse="Replace">
            <remove statusCode="404" subStatusCode="-1" />
            <error statusCode="404" subStatusCode="-1" responseMode="ExecuteURL" path="/404.asp" />
        </httpErrors>
    </system.webServer>
</configuration>
johna
  • 10,540
  • 14
  • 47
  • 72
  • 1
    If you're on IIS7 or above you don't need to mess around with custom error pages, you only need web.config. (Or you can genarate your rules with the Rewrite module in IIS manager, which writes to web.config.) – John Dec 18 '13 at 01:15
  • @John, are we talking Classic ASP here, not .NET? – johna Dec 18 '13 at 04:34
  • 1
    Yes, I'm talking about Classic ASP. Web.Config redirects work with Classic ASP pages (or .htm pages for that matter) in IIS7 or IIS8. I would guess you need to enable .net in your app pool for this to work, but the rules are certainly not restricted to .net pages. Perhaps we should change our usernames. These comments probably look like someone talking to himself :) – John Dec 18 '13 at 14:57
  • @John (the other one), so you can use a .NET Rewrite module to redirect to a Classic ASP page with parameters added to the querystring? That sounds like a useful alternative. – johna Dec 18 '13 at 21:40
  • 1
    Yes, you can have a querystring in your url – John Dec 19 '13 at 11:22