0

I have url like

domain.com/posts/id/title

I want to change URL

domain.com/title-xxx-id

id and title querystring parameters. -xxx- is static.

I had used

 <rule name="Redirect to WWW" stopProcessing="true">

  <match url=".*" />

  <conditions>

    <add input="{HTTP_HOST}" pattern="^olddomain.com$" />

  </conditions>

  <action type="Redirect" url="https://newdomain.com/{R:0}"

       redirectType="Permanent" />

</rule>

to domain change but now I need url change in same domain and I have two parameters

1 Answer 1

1

In the same domain the redirect is even simpler using URLRewrite (2.x). Something like this.

  <rule name="friendly" stopProcessing="true">
    <match url="^post/(.+)/(.+)$" negate="false" />
    <action type="Redirect" url="{R:1}-xxx-{R:2}" appendQueryString="false" />
  </rule>

But in fact you need to send title-xxx-id url to a handler to process. You can do it like this (supposing post is the controller you use).

  <rule name="friendly1" stopProcessing="true">
    <match url="^(.+)-xxx-(\d+)$" negate="false"/>
    <action type="Rewrite" url="/post?title={R:1}&amp;id={R:2}" appendQueryString="false"/>
  </rule>

In fact, both rules can work together.

  <rule name="friendly" stopProcessing="false">
    <match url="^post/(.+)/(.+)$" negate="false" />
    <action type="Redirect" url="{R:1}-xxx-{R:2}" appendQueryString="false" />
  </rule>
  <rule name="friendly1" stopProcessing="true">
    <match url="^(.+)-xxx-(\d+)$" negate="false"/>
    <action type="Rewrite" url="/post?title={R:1}&amp;id={R:2}" appendQueryString="false"/>
  </rule>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much . I have one more question. After this change I have a new problem. Can you help me ? stackoverflow.com/questions/47628919/asp-net-url-rewrite-rule

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.