Click here to Skip to main content
15,887,246 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can you set up a redirect that will do this:

olddomain.com -> newdomain.com/oldsite

AND do this:

olddomain.com/anyusefuldir -> newdomain.com/anyusefuldir

I guess I just need to detect if the url has any file or directory other than index...


What I have tried:

I cant figure out what to try other than
<rule name="Redirects olddomain.com" patternSyntax="ECMAScript" stopProcessing="true">
		            <match url="^(.*)$" ignoreCase="false" />
		            <conditions logicalGrouping="MatchAny">
		                <add input="{HTTP_HOST}" pattern="^olddomain.*(com|org)$" />
		                <add input="{HTTP_HOST}" pattern="^(www.)?olddomain.(com|org)$" />
		            </conditions>
		            <action type="Redirect" url="https://newdomain.org/{R:0}" />
		        </rule>
Posted
Updated 9-May-23 22:36pm
Comments
Scott Dornseif 10-May-23 8:05am    
Thank you Richard Deeming! That is exactly what I needed.

1 solution

How about:
XML
<rule name="Redirects olddomain.com/anyusefuldir" patternSyntax="ECMAScript" stopProcessing="true">
    <match url=".+" />
    <conditions>
        <add input="{HTTP_HOST}" pattern="^(www\.)?olddomain\.(com|org)$" />
    </conditions>
    <action type="Redirect" url="https://newdomain.org/{R:0}" />
</rule>

<rule name="Redirects olddomain.com" patternSyntax="ECMAScript" stopProcessing="true">
    <match url=".+" negate="true" />
    <conditions>
        <add input="{HTTP_HOST}" pattern="^(www.)?olddomain.(com|org)$" />
    </conditions>
    <action type="Redirect" url="https://newdomain.org/oldsite" />
</rule>
The first rule matches when there is a path; the second matches when you navigate to the root of the site.

And you don't need two conditions; the one pattern will match with or without the www. prefix. But you do need to escape the . character, as it has special meaning in a regular expression.

URL Rewrite Module Configuration Reference | Microsoft Learn[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900