Redirect & re-write rules with web.config
Step-by-step tutorial. How to set web.config rules [Redirect & re-write]
- Redirect HTTPS to HTTP
- Redirect HTTP to HTTPS
- Redirect old domain name to new domain name
- Redirect old domain name to new domain name and keep the rest of URL the same when redirecting to the new domain name
- Redirect specific page to another specific page
- Redirect non-WWW to WWW
- Force add WWW to the domain name
- Force remove WWW from domain name
* When you copy one the codes below to your web.config file make sure to past it inside re-write element { <rewrite> [copied code] </rewrite> }
1.Redirect HTTPS to HTTP
- Domain name = https://mydomain.com/
1 2 3 4 5 6 7 8 |
<rule name="Redirect to http" stopProcessing="true"> <match url="(.*)" /> <conditions> <add input="{R:1}" pattern="^onepage/(.*)$" negate="true" /> <add input="{HTTPS}" pattern="^ON$" /> </conditions> <action type="Redirect" url="http://{HTTP_HOST}/{R:0}" redirectType="Permanent" /> </rule> |
* This will redirect https to http
Example:
- https://mydomain.com/ >>> http://mydomain.com/
- https://mydomain.com/somepage/ >>> http://mydomain.com/
2.Redirect HTTP to HTTPS
- Domain name = http://mydomain.com/
1 2 3 4 5 6 7 |
<rule name="Redirect to https" stopProcessing="true"> <match url=".*" /> <conditions> <add input="{HTTPS}" pattern="off" ignoreCase="true" /> </conditions> <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" /> </rule> |
* This will redirect http to https
Example:
- http://mydomain.com/ >>> https://mydomain.com/
- http://mydomain.com/somepage/ >>> https://mydomain.com/
3.Redirect old domain to new domain
- Old domain = http://olddomain.com/
- New domain = http://newdomain.com/
1 2 3 4 5 6 7 |
<rule name="Standard redirection" stopProcessing="true"> <match url=".*" /> <conditions> <add input="{HTTP_HOST}" pattern="^olddomain.com$" /> </conditions> <action type="Redirect" url="http://newdomain.com/{R:0}" redirectType="Permanent" /> </rule> |
* This will redirect your old domain name to your new domain name when user type the old domain name in browser.
Example:
- http://olddomain.com/ >>> http://newdomain.com/
- http://olddomain.com /somepage/ >>> http://newdomain.com/
4.Redirect old domain to new domain and keep the rest of URL when redirect to new domain
- Old domain = http://olddomain.com/
- New domain = http://newdomain.com/
1 2 3 4 5 6 7 |
<rule name="Advanced redirection"> <match url="(.*)" /> <conditions> <add input="{HTTP_HOST}" pattern="www\.olddomain\.com$" negate="true" /> </conditions> <action type="Redirect" url="https://newdomain.com/{R:1}" /> </rule> |
* This will redirect your old domain name to your new domain name when user type the old domain name in browser and it will keep the rest of URL from the old domain name and post it to the new redirected domain name.
Example:
- http://olddomain.com/ >>> https://newdomain.com/
- http://olddomain.com /somepage/ >>> https://newdomain.com/somepage/
5.Redirect specific page to another specific page
- Page one = http://mydomain.com/pageone
- Page two = http://mydomain.com/pagetwo
1 2 3 4 5 6 7 8 |
<rule name="Redirect specific page to another specific page" stopProcessing="true"> <match url="(.*)" /> <conditions logicalGrouping="MatchAny" trackAllCaptures="false"> <add input="{HTTP_HOST}{REQUEST_URI}" pattern="mydomain.com/pageone" /> <add input="{HTTP_HOST}{REQUEST_URI}" pattern="www.mydomain.com/pageone" /> </conditions> <action type="Redirect" url="http://mydomain.com/pagetwo" redirectType="Permanent"/> </rule> |
* This will redirect [pageone] to [pagetwo] when user try to access page one
Example:
- http://mydomain.com/pageone >>> http://mydomain.com/pagetwo
- http://www.mydomain.com/pageone >>> http://mydomain.com/pagetwo
6.Redirect non WWW to WWW
- Domain name = http://mydomain.com/
1 2 3 4 5 6 7 |
<rule name="Redirect non WWW to WWWW" stopProcessing="true"> <match url=".*" /> <conditions> <add input="{CACHE_URL}" pattern="^(.+)://(?!www)(.*)" /> </conditions> <action type="Redirect" url="{C:1}://www.{C:2}" redirectType="Permanent" /> </rule> |
* This will redirect non WWW domain name to WWW one
Example:
- http://mydomain.com/ >>> http://www.mydomain.com/
- http://www.mydomain.com/ >>> http://www.mydomain.com/
7.Force add WWW to domain name
- Domain name = http://mydomain.com/
1 2 3 4 5 6 7 |
<rule name="Force add WWW to domain name" enabled="true"> <match url="(.*)" /> <conditions> <add input="{HTTP_HOST}" negate="true" pattern="^www\.(.*)$" /> </conditions> <action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" appendQueryString="true" redirectType="Permanent" /> </rule> |
* This will force add WWW to your domain name when user type it like this: http://mydomain.com/
Example:
- http://mydomain.com/ >>> http://www.mydomain.com/
- http://www.mydomain.com/ >>> http://www.mydomain.com/
8.Force remove WWW from domain name
- Domain name = http://www.mydomain.com/
1 2 3 4 5 6 7 |
<rule name="Force remove WWW from domain name" stopProcessing="true"> <match url=".*" ignoreCase="true" /> <conditions logicalGrouping="MatchAll"> <add input="{HTTP_HOST}" pattern="^www\.(.*)$" /> </conditions> <action type="Redirect" url="http://{C:1}/{R:0}" appendQueryString="true" redirectType="Permanent" /> </rule> |
* This will force remove WWW from your domain name when user type it like this: http://www.mydomain.com/
Example:
- http://www.mydomain.com/ >>> http://mydomain.com/
- http://mydomain.com/ >>> http://mydomain.com/
Good luck 🙂
Helpful information. Lucky me I found your web site unintentionally,
and I’m stunned why this coincidence did not happened in advance!
I bookmarked it.