IIS URL重写ASP

时间:2013-12-17 19:09:47

标签: iis asp-classic url-rewriting

我会尽力扫描论坛以获取帮助,使web.config能够重写这种网址

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

我希望这可以是

domain.com/en/service

其中language=2是“en” id=3是“服务”页面(此名称存在于mySQL中)

我只能找到相反的例子......

喜欢这个

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

我希望它是这样的......我知道这不正确,但也许可以解释我的问题。

<rewrite>
  <rules>
    <rule name="enquiry" stopProcessing="true">
     <match url="^default.asp?id=3&language=2$" />
     <action type="Rewrite" url="/en/serice" />
    </rule>
  </rules>
</rewrite>

2 个答案:

答案 0 :(得分:1)

我使用自定义错误页面在Classic ASP中完成此操作,这似乎是最佳方式,除非您使用服务器上安装的某种第三方组件。

为此,在IIS(或web.config)中,您需要设置404错误以转到特定的自定义错误Classic ASP页面(例如404.asp)。

在此自定义错误页面中,您首先需要检查URL是否有效。如果是,您可以将Server.Transfer转到正确的页面,返回200响应代码,并在那里解析URL以将URL转换为数据库查找所需的值,等等。如果它不是有效的URL,那么您显示自定义错误页面并返回404响应代码。

检查有效网址和检索网址参数的代码会因您的网址结构而异。但要查找自定义404错误页面上请求的URL,您必须查看查询字符串,这将类似于“404; http://domain.com:80/en/service/”。

以下是从请求的网址获取参数的示例代码:

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

以下是如何在web.config中设置自定义错误页面(而不是在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>

答案 1 :(得分:1)

如果你想使用正则表达式,你可以做这样的事情

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

这将改写&#34; domain.com/en/service" as&#34; domain.com/default.asp?language = en&amp; id = Service&#34;,或&#34; domain.com/2/3" as&#34; domain.com/default.asp?language = 2&amp; id = 3&#34;

要将2更改为en和3更改为服务以及所有其他选项,但我认为您需要为每个排列设置单独的规则,或者在您的asp页面中使用某种逻辑来读取查询字符串变量和将相应的值发送到SQL查询。另请注意,友好URL中的参数在重写的URL中以相同的顺序和查询字符串变量出现,尽管这不应该是一个问题。如果有人试图访问原始&#34;不友好&#34;他们会找到他们正在寻找的东西,无论他们进入查询字符串变量的方式。

请注意,我实际上并没有手动编写上面的规则,我使用IIS管理器中的URL重写模块生成它 - 它使生活更轻松

另请注意,正如我在其他答案中的同名所讨论的,这仅适用于IIS7及以上