简体   繁体   中英

Append Query String for a specific page in Rewrite Rule Web.config

I want to add open the following page http://test.com/Collection.aspx?title=Women

as

http://tests.com/Women

and

http://tests.com/Collection.aspx?title=Women

as

http://test.com/pathann

I have tried to use below rewrite rules but these are working for all page, i just want to implement for this specific section.

 <rule name="RedirectUserFriendlsssyURL1" stopProcessing="true">
      <match url="^Collection\.aspx$" />
      <conditions>
        <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
        <add input="{QUERY_STRING}" pattern="^title=([^=&amp;]+)$" />
      </conditions>
      <action type="Redirect" url="{C:1}" appendQueryString="false" />
    </rule>
      <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="Collection.aspx?title={R:1}" />
    </rule>

Please help me how can i do it for these specific page only. Actually if i apply for all pages, it is blocking some other functionality to work.

What I will do in your case is to leave only the redirect part to the url rewrite module:

<rule name="RedirectUserFriendlsssyURL1" stopProcessing="true">
   <match url="^Collection\.aspx$" />
   <conditions>
     <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
     <add input="{QUERY_STRING}" pattern="^title=([^=&amp;]+)$" />
   </conditions>
   <action type="Redirect" url="{C:1}" appendQueryString="false" />
</rule>

And handle the rest by routing in your global.asax:

void Application_Start(object sender, EventArgs e)
{
    RegisterRoutes(RouteTable.Routes);
}

void RegisterRoutes(RouteCollection routes)
{
    routes.MapPageRoute("CollectionRoute",
        "{title}",
        "~/Collection.aspx", false,
        new RouteValueDictionary(),
        //Here you can define the regex pattern to match the title phrases
        new RouteValueDictionary {
            { "title", "(women)|(pathann)" }
        });
}

But of course if you still prefer to leave everything handled by url rewrite module, you can define the rule like this:

<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
      <match url="(women)|(pathann)" />
      <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
      </conditions>
      <action type="Rewrite" url="Collection.aspx?title={R:1}" />
</rule>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM