URL 友好 ASP Classic 和 Isapi Rewrite

URL friendly ASP Classic and Isapi Rewrite

关键是,我可以访问地址 dominio.com/modulo/id/titulo 并将其重写为 dominio.com/default.asp?link=artigo&id=123&titulo=teste,但我的问题是我是否可以执行相反的过程,即转到 dominio.com/default.asp?link=artigo&id=123&titulo=teste 并它变为 dominio.com/modulo/id/titulo.

代码:

ASP

<!DOCTYPE html><html lang="pt-br"><head><meta charset="utf-8"/><title>Teste Isapi Rewrite</title></head><body><p>Teste!<br>link: <%=request("link")%><br>id: <%=request("id")%><br>teste: <%=request("teste")%><br></p></body></html>

WEB.CONFIG

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

<system.webServer>
    <rewrite>
        <rules>
            <rule name="artigo" stopProcessing="true">
                <match url="^artigo/?([a-zA-Z0-9_-]+)?/?([a-zA-Z0-9_-]+)?/?([a-zA-Z0-9_-]+)?$" />
                <conditions> 
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> 
                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> 
                </conditions>
                <action type="Rewrite" url="default.asp?link={R:0}&amp;id={R:1}&amp;teste={R:2}" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>

</configuration>

您可以使用以下 url 重写规则:

 <rule name="reverse" enabled="true" stopProcessing="true">
                <match url="(.*)" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    <add input="{REQUEST_URI}" pattern="default.asp" />
                    <add input="{QUERY_STRING}" pattern="link=(.*)\&amp;id=(.*)\&amp;titulo=(.*)" />
                </conditions>
                <action type="Redirect" url="http://{HTTP_HOST}/{C:1}/{C:2}/{C:3}" appendQueryString="false" />
            </rule>


            <rule name="RewriteUserFriendlyURL1" enabled="true" stopProcessing="true">
                <match url="^([^/]+)/([^/]+)/([^/]+)/?$" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                </conditions>
                <action type="Rewrite" url="default.asp?link={R:1}&amp;id={R:2}&amp;titulo={R:3}" appendQueryString="false" />
            </rule>

默认页面代码:

<!DOCTYPE html><html lang="pt-br">
<head>
<meta charset="utf-8"/>
<title>Teste Isapi Rewrite</title>
</head>
<body>
<p>Teste!<br>link: <%=request("link")%><br>id: <%=request("id")%><br>titulo: <%=request("titulo")%><br></p>
</body>
</html>

我认为您想要的是一条将 "unfriendly" URL 重定向到 "friendly" 的规则 - 即,如果您将不友好的粘贴到浏览器地址栏中,那么当您的页面出现在屏幕上时,它会变为友好的。 Janvi Panchal 在上面有正确的想法。你所做的是有一个重定向规则和一个重写规则如下。

<rule name="RedirectUserFriendlyURL1" stopProcessing="true">
     <match url="^dominio\.com/default\.asp$" />
     <conditions>
        <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
        <add input="{QUERY_STRING}" pattern="^link=([^=&amp;]+)&amp;id=([^=&amp;]+)&amp;titulo=([^=&amp;]+)$" />
     </conditions>
     <action type="Redirect" url="dominio.com/default/{C:1}/{C:2}/{C:3}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
     <match url="^dominio\.com/default/([^/]+)/([^/]+)/([^/]+)/?$" />
     <conditions>
         <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
         <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
     </conditions>
     <action type="Rewrite" url="dominio.com/default.asp?link={R:1}&amp;id={R:2}&amp;titulo={R:3}" />
 </rule>

请注意,重定向规则排在第一位,因此发生的情况是该位置被重定向到友好的 URL,然后被重写为不友好的。

另请注意,我没有手动编写代码 - 我使用 IIS 管理器中的 URL 重写向导生成了它。单击 URL 重写图标,然后单击添加规则,然后单击用户友好 URL。在单击“确定”按钮之前检查 "Create corresponding redirect rule",此代码将生成并附加到 web.config 文件中适当的位置