开发者

Classic ASP URL Rewriting

I was wondering if it is possible to do URL Rewriting with Classic ASP when you do not have access to IIS to make any rewrite changes?

We have an online shop where products are typically linked as /product.asp?ContentID=X but would like to have something more SEO friendly such as /product/unique-product-name

Unfortunately we are on a shared hosting platform and are current hosting provider is not willing to install additional components to their server in case they c开发者_如何学运维ause issues for others. :(


A later answer but could help you, or others.

I sometimes still code in classic ASP, and also struggled with URL rewriting for a while because of shared hosting etc.

When I did have access to IIS settings, either by myself manually, or by asking the server support team, I used the 404 error method. Although this worked, it wasn't great. It was slightly slower than the "normal" way, and it clogged up my error reports like mad.

Since IIS7 came out, I found a hosting provider that offered the use of the IIS7 URL Rewrite Module (for me it was GoDaddy), and all I have to do now is upload a web.config file to the root of my application, and URL rewriting works perfectly.

So my advice is, if you can, move your application to a hosting provider that offers this service. If and when you do change hosts, all you would need is a web.config file (below), upload it to the root and you're done. You won't need to contact the host at all.

Save this as web.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Rewrite to friendly URL">
                      <match url="^blog/([0-9]+)/([_0-9a-z-]+)" />
                      <action type="Rewrite" url="blog/article.asp?id={R:1}&amp;title={R:2}" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

If somebody enters:

http://mydomain.com/blog/1/the-article-title

...it will be rewritten to:

http://mydomain.com/blog/article.asp?id=1&title=the-article-title

Enjoy


Probably not.

You should, at least, alter IIS configuration for 404 pages, so you code could take control.


I've done this. It's kludgy but workable.

As another commenter mentioned you can have a custom 404 page. You'll need at least a little cooperation from your hosting provider to alter IIS to send 404 errors to a custom page in your directory. They shouldn't have a problem with that; won't affect other users.

As far as I can tell, on your custom 404 ASP page, about all you really have access to is the original URL via Request.ServerVariables("QUERY_STRING")

You can then parse the contents of that out and redirect to whatever is needed with Server.Transfer (not Response.Redirect, because you want a seamless server-side redirect and not a client-side redirect)

Make sure you have a fall-though case to handle actual 404 errors too!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜