How to Create Rewrite Rule in web.config

If you need a domain URL redirection from yourdomain.com to www.testdomain.com or vise versa, you can do something like this:\r\n\r\nPlace either of these (depending on what you’d like done. And edit to match your domain) inside the <system.webServer></system.webServer> tags in the web.config of the domain.\r\n

<rewrite><rules>\r\n<rule name=”Add WWW prefix” >\r\n<match url=”(.*)” ignoreCase=”true” />\r\n<conditions>\r\n<add input=”{HTTP_HOST}” pattern=”^testdomain\.com” />\r\n</conditions>\r\n<action type=”Redirect” url=”http://www.testdomain.com/{R:1}”\r\nredirectType=”Permanent” />\r\n</rule>

\r\n—————–\r\n

<rule name=”Remove WWW prefix” >\r\n<match url=”(.*)” ignoreCase=”true” />\r\n<conditions>\r\n<add input=”{HTTP_HOST}” pattern=”^www\.testdomain\.com” />\r\n</conditions>\r\n<action type=”Redirect” url=”http://yourdomain.com/{R:1}”\r\nredirectType=”Permanent” />\r\n</rule>\r\n</rules></rewrite>

\r\n

Note: This is applicable on websites hosted on IIS 7.0 or 7.5 (on Windows Server 2008).

2 thoughts on “How to Create Rewrite Rule in web.config”

  1. Google Chrome 9.0.597.98 Google Chrome 9.0.597.98 Windows Server 2003 Windows Server 2003
    Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US) AppleWebKit/534.13 (KHTML, like Gecko) Chrome/9.0.597.98 Safari/534.13

    This does not work on IIS 6.0. This is only for IIS 7.0, please write it down at the top of the post.

  2. Google Chrome 9.0.597.98 Google Chrome 9.0.597.98 Windows 7 Windows 7
    Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.13 (KHTML, like Gecko) Chrome/9.0.597.98 Safari/534.13

    It’s updated.\nThank you Abaid, I appreciate your contribution, really.\n

Comments are closed.