1

in existing production we have URL structure as follows https://mainsite.com/subsite

Now the sub site URL structure changed as follows https://mainsite.com/newsubsite/subsite2

We want to redirect old sub site landing page and all the inner pages to redirect to new url structure I.e., when user browse https://mainsite.com/subsite it should redirect to https://mainsite.com/newsubsite/subsite2

And when user browse https://mainsite.com/subsite/innerpage it should redirect to https://mainsite.com/newsubsite/subsite2/innerpage

The inner page name don't have any name changes. Just need to replace "subsite" with "newsubsite/subsite2"

Edit: one more scenario need to handle. Some times language code e.g.: "en" will come in the URL as like below https://mainsite.com/en/subsite

In this case it need to redirect to https://mainsite.com/en/newsubsite/subsite2

Same for inner pages as well

Any body help me to create a generic rewrite rule to do this job.

2 Answers 2

2

A simple rule like that will do the trick:

<rule name="subsite rule" stopProcessing="true">
    <match url="^([a-z]{2}(\-[a-z]{2})?/)?subsite(/.*){0,1}$" />
    <action type="Redirect" url="/{R:1}newsubsite/subsite2{R:3}" appendQueryString="true" />
</rule>

Remember to recycle app pool after you edit your config - simple site restart related to config change is not enough usually.

Match is for the urls starting (optionally with language code either 2 letters like en or 4 letters with hyphen like en-us), and then with subsite and then either nothing after it or urls starting with subsite/, e.g.

https://mainsite.com/subsite
https://mainsite.com/subsite/
https://mainsite.com/subsite/subfolder
https://mainsite.com/subsite/sub/sub/sub
https://mainsite.com/en/subsite
https://mainsite.com/en-us/subsite/
https://mainsite.com/en-es/subsite/subfolder
https://mainsite.com/en/subsite/sub/sub/sub

Whatever comes after subsite will be added to resulting url after /newsubsite/subsite2.

appendQueryString="true" makes sure that if there is query string, it will be included in the new url as well.

3
  • this is perfectly working fine. but I found one more scenario and added to the question. your help is very much appreciated
    – shahul
    Commented May 26, 2023 at 9:33
  • answer updated.
    – Marek Musielak
    Commented May 26, 2023 at 12:05
  • Thanks. it worked perfectly.
    – shahul
    Commented May 29, 2023 at 6:40
1

You can redirect your existing production we have URL to sub site URL as below

<rule name="ms1" stopProcessing="true"> 
<match url="^subsite/?$" /> 
<conditions logicalGrouping="MatchAny"> <add input="{HTTP_HOST}" 
pattern="^(mainsite.com|www.mainsite.com)$" /> </conditions> 
<action type="Redirect" url="https://mainsite.com/newsubsite/subsite2" /> </rule>

I tested this with my instance URL and it is working as expected.

Hope this helps.

3
  • this will not append inner page urls
    – shahul
    Commented May 26, 2023 at 10:29
  • have you tried this, is it not appending inner page urls? Commented May 26, 2023 at 13:51
  • Hi @Rishiraj, thanks you for you help. Marek answer is working for me. so I have used that.
    – shahul
    Commented May 29, 2023 at 6:43

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.