- The article explains how to use IIS to redirect requests from a local domain to an external website, without changing the URL in the browser.
- The article provides step-by-step instructions on how to configure the redirection using the Home Directory, Virtual Directory, or Directory tab in IIS Manager, and how to test the redirection using curl or other tools.
- The article also answers some frequently asked questions related to the redirection, such as how to redirect only certain requests, how to preserve the original path and query string, and how to redirect to a different port or protocol.
In this article, we will explore how to use Internet Information Services (IIS) to redirect requests from a local domain to an externally hosted website, without changing the URL in the browser. This can be useful for various scenarios, such as:
- Hosting a landing page or a blog on a third-party platform, while keeping your own domain name.
- Migrating your website to a new hosting provider, while minimizing downtime and SEO impact.
- Testing or staging your website on a different server, while keeping the original URL for convenience.
We will use the example of a fictional company called Contoso, which wants to redirect its local domain www.contoso.com to an external website hosted on www.example.com. We will assume that Contoso has already configured its DNS records to point www.contoso.com to its own IIS server.
How to Redirect Requests Using IIS
To redirect requests from a local domain to an external website using IIS, we need to follow these steps:
- Open IIS Manager and expand the local computer node.
- Right-click on the website or directory that you want to redirect, and click Properties.
- Click on the Home Directory, Virtual Directory, or Directory tab, depending on the type of your website or directory.
- Under The content for this source should come from, select A redirection to a URL.
- In the Redirect to box, type the URL of the external website that you want to redirect to. For example, http://www.example.com.
- Optionally, you can check the following options:
- The exact URL entered above: This will redirect the request to the exact URL that you specified, without appending any additional path or query string information. For example, if you request http://www.contoso.com/products, you will be redirected to http://www.example.com, not http://www.example.com/products.
- A permanent redirection for this resource: This will send a 301 Moved Permanently status code to the browser, indicating that the resource has been permanently moved to a new location. This can help with SEO and caching, but it also means that you cannot easily change the redirection later.
- A temporary redirection for this resource: This will send a 307 Temporary Redirect status code to the browser, indicating that the resource is temporarily available at a different location. This can allow you to change the redirection later, but it also means that the browser may not cache the redirection and may request it again.
- Click OK to save the changes and apply the redirection.
How to Test the Redirection
To test if the redirection is working correctly, you can use any web browser or tool that can send HTTP requests and inspect the response headers. For example, you can use curl, a command-line tool that can transfer data using various protocols.
To test the redirection using curl, you can use the following command:
curl -I http://www.contoso.com
This will send a HEAD request to http://www.contoso.com and display only the response headers. You should see something like this:
HTTP/1.1 301 Moved Permanently Content-Length: 156 Content-Type: text/html Location: http://www.example.com Server: Microsoft-IIS/10.0 Date: Fri, 20 Oct 2023 07:37:19 GMT
The response headers show that the request was redirected with a 301 status code and a Location header pointing to http://www.example.com. This means that the browser will automatically follow the redirection and load the content from http://www.example.com, while keeping http://www.contoso.com in the address bar.
Frequently Asked Questions
Here are some common questions and answers related to using IIS to redirect requests to an externally hosted website.
Question: How can I redirect only certain requests based on some criteria?
Answer: You can use URL Rewrite, an IIS extension that allows you to create powerful rules to modify and redirect requests based on various criteria, such as URL patterns, query strings, headers, cookies, server variables, and more.
Question: How can I preserve the original path and query string when redirecting requests?
Answer: You can use a special placeholder called $V$ in the Redirect to box, which will be replaced by the original path and query string of the request. For example, if you want to redirect http://www.contoso.com/products to http://www.example.com/products, you can use http://www.example.com$V$ as the Redirect to value.
Question: How can I redirect requests to a different port or protocol?
Answer: You can specify the port or protocol in the Redirect to box, along with the host name and the path. For example, if you want to redirect http://www.contoso.com to https://www.example.com:443, you can use https://www.example.com:443 as the Redirect to value.
Disclaimer: This article is for informational purposes only and does not constitute professional advice. The author and the publisher are not liable for any damages or losses that may result from following the instructions or using the tools mentioned in this article. Always consult a qualified expert before making any changes to your IIS configuration or website hosting.