skip to Main Content

The point is, I can access an address dominio.com/modulo/id/titulo and it rewrites to dominio.com/default.asp?link=artigo&id=123&titulo=teste, but my question is whether I can do the reverse process, i.e. go to dominio.com/default.asp?link=artigo&id=123&titulo=teste and it changes to dominio.com/modulo/id/titulo.

Codes:

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>

2

Answers


  1. You could use below url rewrite rule:

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

    default page code:

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

    enter image description here

    Login or Signup to reply.
  2. What I think you’re after is a rule which redirects the “unfriendly” URL to the “friendly” one – ie if you paste the unfriendly one into your browser address bar then it changes to the friendly one when your page appears in the screen. Janvi Panchal has the right idea above. What you do is have a redirect rule and a rewrite rule as follows.

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

    Note that the redirect rule comes first, so what’s happening is that the location is redirected to the friendly URL which is then rewritten as the unfriendly one.

    Also note that I didn’t hand code this – I generated it with the URL rewrite wizard in IIS Manager. Click on the URL Rewrite icon, then Add Rule then User Friendly URL. Check the “Create corresponding redirect rule” before you click the OK button and this code will be generated and appended to your web.config file in the appropriate location

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search