Hi there!!

I would like to know if is possible to have routing in a website made with classic asp...

In stead of showing this web address :
www.website.com/mypage.asp?id=1

i would love to show this one (like MVC):
www.website.com/mypage/1

Is it possbile?

Thanks to all

Dani AI

Generated

Yes, you can get clean URLs in Classic ASP. ’s 404-catch idea works well: configure IIS to send 404s to a dynamic page (router.asp) using “Execute URL,” not a 302 redirect. That lets Classic ASP inspect the originally requested path and route server-side. See the IIS docs for configuring custom error pages with Execute URL. IIS httpErrors custom errors. (learn.microsoft.com)

Example router that maps /mypage/1 to /mypage.asp?id=1 and returns 200 on success:

' router.asp (set as the 404 "Execute URL")
<%
Response.Buffer = True

Dim path, re, m, id
path = LCase(Request.ServerVariables("PATH_INFO"))

Set re = New RegExp
re.IgnoreCase = True
re.Global = False

' /mypage/1 -> /mypage.asp?id=1
re.Pattern = "^/mypage/([0-9]+)$"
Set m = re.Execute(path)
If m.Count > 0 Then
  id = m(0).SubMatches(0)
  Response.Clear
  Response.Status = "200 OK"
  Server.Transfer("/mypage.asp?id=" & id) 'server-side; URL stays pretty
End If

' No match: return a real 404
Response.Status = "404 Not Found"
Server.Transfer("/404.asp")
%>

Note the use of Server.Transfer so the browser URL does not change and there is no extra round-trip. Server.Transfer (Classic ASP). (learn.microsoft.com)

If you control IIS, ’s alternative is cleaner: install URL Rewrite and add a rule to web.config so no 404 trick is needed:

<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="mypage-id" stopProcessing="true">
          <match url="^mypage/([0-9]+)$" />
          <action type="Rewrite" url="/mypage.asp?id={R:1}" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

This runs before Classic ASP and works with any app type. Creating rewrite rules, URL Rewrite overview. (learn.microsoft.com)

Tip: if you maintain a lot of friendly URLs, consider a rewrite map so you can update routes without touching code. Rewrite module configuration reference. (learn.microsoft.com)

Recommended Answers

All 5 Replies

A common approach to url rewriting in classic asp is a work-around. For this to work, you will need to have access to a custom 404 page. In other words, you will depend on IIS to send 404 errors to a custom page in your directory. Most providers have this option for their customers.

Then, when the user arrives to this custom 404 error page, you leverage asp to get the URL via Request.ServerVariables. You then simply parse the contents of that out and use Server.Transfer to provide the correct content back to the user. Dont use Response.Redirect because you want the redirect to happen server-side and not client-side.

Ok... i'm not sure i understand... can you show me an example of code?

Thanks ^_^

I dont have a working example to provide you with..but the general idea is that you want for your web server to take you to a custom asp page when the result of the URL is page not found (404 error). Then on that custom page, you use asp code to get the URL.

it is a bit complicated to explain, but once you understand the concept, it is fairly easy to implement.

I found this article that could help you set this up.

http://she7ata.com/blogs/articles/14/URL+rewritings+on+Classic+ASP+3.0+without+IIS+configurations.html

Although this article shows how to include the target content, the suggestion I gave above is slightly different because I suggested the use of Server.Transfer to perform a redirect on the server and provide the correct content back to the user.

There are a few variations/techniques on how to do this with classic ASP. Once you understand the general approach, you just need to do some testing on your end.

Thanks!! that's perfect

I did what JorgeM suggested on one of my sites to catch 404's and figure out the best page to send the user, but you can also write a script to generate a set of URL rewrite rules for a web.config file. I have done this on same site but it requires running of the script and updating of the webconfig file everytime stuff is added or removed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.