Link to home
Start Free TrialLog in
Avatar of MadDog986
MadDog986

asked on

asp.net RewritePath in classic asp?

I have the following asp.net script that works great for what i need. The only problem is that i cant get it to work with normal classic asp files. What i need is something that works just like the asp.net code but for classic asp.

Thanks in advanced for your help. This one has left me clueless for some time now.

asp.net script:
----
<script language="C#" runat="server">

protected void Application_BeginRequest(Object sender, EventArgs e)
{
      HttpContext incoming = HttpContext.Current;
      string oldpath = incoming.Request.Path.ToLower();

      string pageid; // page id requested

      // Regular expressions to grab the page id from the pageX.aspx
      Regex regex = new Regex(@"topic(\d+).aspx",  RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
      MatchCollection matches = regex.Matches(oldpath);

      if (matches.Count > 0)
      {
            // Extract the page id and send it to Process.aspx
            pageid = matches[0].Groups[1].ToString();
            incoming.RewritePath("forum_posts.asp?TID=" + pageid);
      } else {
            // Display path if it doesn’t containt pageX.aspx
            incoming.RewritePath(oldpath);
      }
}

</script>
Avatar of Mike_Metro
Mike_Metro

Avatar of MadDog986

ASKER

I need to be able to create this would using a DLL or some kind of add-on for IIS.

This has to be done with normal code.
Read this article.  It goes into the details a little.  
http://www.webmasterworld.com/forum47/64-2-10.htm

- Change the 404 error page to a script you create. (ex.  Redirect404.asp)
- In the Redirect404.asp page have similar logic to your aspx page.  
  - Read the current request url
  - Set the status to 200
  - Server.execute or response.redirect to the appropriate page based on the url.

It's not nearly as nice as in ASP.NET or other web languages.  Without using ISAPI, this is really the only way to do it.
All that script does is a redirect. I am trying to turn my urls into something more cleaner so search engines can index my site better.

Example:

I want the page to query http://www.site.com/page?ID=1&Page=Test but the URL will show something like http://www.site.com/page/1/test


Keep in mind am i using a shared host. So dlls or add-ons for IIS is out of the question. The best thing i found that works is the asp.net example above but for some reason it will not except regular .asp extensions for the RewriteURL.
I'm not sure if this will work.  But give it a try.  The regular expression is only checking for aspx files.  Try changing it to:

     Regex regex = new Regex(@"topic(\d+).asp[x]?",  RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
I still get a asp.net error when trying to run the script:

"This type of page is not served."
If this helps, what i am trying to do is hide the old URLs with new ones without having to install some special component or use a IIS url filter.

Basically what i want to do is turn:

http://www.site.com/forum/forum_posts.asp?TID=(number)

into something like

http://www.site.com/forum/forum_posts/(number) or http://www.site.com/forum/1/forum_posts.asp

If anyone knows a way to do this please help me.
Many ISPs allow you to create custom 404 error pages.  Check your ISP docs and their control panel if you have one.  That's really going to be the only way to do it without installing a special component.

All 404 pages do is allow you to do a redirect. I cant do a redirect because the URL will not stay the same.
ASKER CERTIFIED SOLUTION
Avatar of Mike_Metro
Mike_Metro

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial