Web.Config重写mysql数据库中名为的URL
我有一个Web.config文件,用于友好地重写url,如下所示:Web.Config重写mysql数据库中名为的URL,mysql,asp-classic,web-config,Mysql,Asp Classic,Web Config,我有一个Web.config文件,用于友好地重写url,如下所示: <configuration> <system.webServer> <rewrite> <rules> // rewrites here <rule name="articles" stopProcessing="true">
<configuration>
<system.webServer>
<rewrite>
<rules>
// rewrites here
<rule name="articles" stopProcessing="true">
<match url="^article/([0-9]+)/?$" ignoreCase="true" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/article.asp?id={R:1}" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
//在这里重写
但是我需要显示mywebsite.com/article/1/数据库中的文章名称,例如mywebsite.com/title article/。
我已经做了一个很好的研究,但我没有找到任何真正解释如何做的东西。我认为web.config不可能做到这一点,因为web.config无法查询数据库。您所能做的就是为articles表中的每个页面创建一个单独的重写规则 您的另一个选项,也是在IIS URL重写模块出现之前使用得更多的选项,是创建一个自定义404页面,并将逻辑放在该页面中 在配置文件的system.webServer部分添加以下内容:
<httpErrors errorMode="Custom">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" prefixLanguageFilePath="" path="/404.asp" responseMode="ExecuteURL" />
</httpErrors>
然后,您可以使用VBScript获取所需URL的一部分,并在数据库查询中使用它。我建议您使用示例中的Url模式
Dim TitleArticle
TitleArticle = Replace(Request.ServerVariables("query_string"),"404;http://mywebsite.com/","")
然后,您可以在数据库查询中使用TitleArticle
查找所需页面,并使用Server.Transfer
将用户带到那里,同时在地址栏中保留友好的url。我建议在测试时使用Response.Write TitleArticle
,以确保将正确的值发送到数据库
显然,这一切都需要在条件语句中,而Else
条件是显示标准404消息
Dim TitleArticle
TitleArticle = Replace(Request.ServerVariables("query_string"),"404;http://mywebsite.com/","")