regexhttp-redirectiispathurl-rewrite-module

IIS web.config redirect pages from one domain to another using specifi paths


I'm trying to figure out if the following is possible on an IIS 7.5 server using web.config rewrite rules. We are moving pages from one domain to another. Here is the configuration.

Old domain page/path example:

https://www.olddomain.com/oldpath/news/2023/10/16/article-name/

New domain page/path example:

https://www.newdomain.com/newpath/news/2023/10/16/article-name/

The differences between the domains/paths are the domain itself and the first level path name. Everything else following the first level path name stays the name.

So basically the rewrite rule would look for a specific path structure /oldpath/news/dateyear/datemonth/dateday/article-name/ and forward via 301 redirect to newdomain.com/newpath/news/dateyear/datemonth/dateday/article-name/. Notice that the second level path name remain the same between domains "/news/".

The redirect would apply to the following examples:

The redirect would need to be path specific "/oldpath/news/"and not apply to pages that exist in non-defined paths. Eg: /about/, /contact/ etc.

Other conditions:

Date folders DO NOT need to be redirected but its okay if they do. Eg:

Please and thank you.


Solution

  • Based on your description, I wrote the following rewrite rule and tested it. It's work for me. I hope it can help you too. Please try the following rule:

    <rewrite>
      <rules>
        <rule name="Redirect to New Domain and Path" stopProcessing="true">
          <match url="^oldpath/news/(\d{4}/\d{2}/\d{2}/.*)$" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^www\.olddomain\.com$" />
          </conditions>
          <action type="Redirect" url="https://www.newdomain.com/newpath/news/{R:1}" redirectType="Permanent" />
        </rule>
      </rules>
    </rewrite>