IIS Configuration - HTTP Redirect
This article explains how to use the HTTP Redirect section in IIS 7 and above.
Contents
What is it for
The HTTP Redirect section is used to do one of the following:
- Redirect a file to a different file, to a different folder, or to a different site.
- Redirect a directory to a file within a different directory, to a different directory, or to a different site.
- Redirect a site to a different site
Bottom line, if you have content that has been moved, and you want to redirect users to the new location of the content, this is a good tool to use.
What it is not for
The HTTP Redirect section is not ideal for doing any of the following:
- It is not for redirecting from "http://domainname.com" to "http://www.domainname.com"
- It is not for redirecting from "http" to "https" or vice versa
- It is not for creating SEO friendly URLs
For all of the above, the proper tools to use are "IIS Rewrite" or "ISAPI_Rewrite"
Configuring through IIS Manager GUI
How to configure HTTP Redirects directly through the IIS Manager GUI.
- Within IIS Manager, choose the specific site, directory, or file you would like to redirect using the 'Sites' tree-menu. For selecting files, you will need to choose the containing folder, select 'Content View' toward the bottom of the window, select the specific file, and click 'Switch to Features View' .
- Choose HTTP Redirect
- Configure the following options:
- Redirect requests to this destination
- Checking this option enables the HTTP Redirect
- Destination Box (The text box immediately below the above option)
- Allows you to specify the destination of the redirect
Redirect Behavior:
- Redirect all requests to the exact destination (instead of relative to destination)
- When this option is checked, browsers will be redirect to the exact URL that was entered in the 'Destination Box' regardless of the original URL.
- When this option is unchecked, the relative path underneath the redirected site or folder will be maintained at the destination.
- Only redirect requests to content in this directory (not subdirectories)
- Checking this option when setting the redirect on a directory will only redirect traffic within the specific directory. Traffic going to sub-directories will not be redirected.
- Leaving this option unchecked will redirect both the current directory and all sub-directories.
- Status Code
- Found (302) - Use this option if the redirect is only temporary. Browsers will not cache the redirect and search engines will continue attempting to index the source.
- Permanent (301) - Use this option if the redirect is permanent. Browsers will cache the redirect and search engines will stop indexing the source and index the destination instead.
- Temporary (307) - Use this option if the redirect is only temporary, and you want POST data to be redirected to the destination.
- Click 'Apply' .
Configuring directly in web.config file
On shared servers, you may not have access to the IIS Manager GUI. In these cases, you can configure the redirect within the control panel (IIS Settings => URL Forwarding) or by directory creating/modifying a 'web.config' file on your site. Below is how to configure the web.config file:
The following is an example of the 'httpRedirect' configuration section:
<configuration> <system.webServer> <httpRedirect enabled="true" destination="http://www.domainname.com/" exactDestination="false" httpResponseStatus="Permanent" /> </system.webServer> </configuration>
The options are the same as those when using the GUI to configure the redirect (see GUI details above). If you want the section to affect only a particular file or sub-directory, use the 'location' tag to specify the location that this configuration section should affect. Example:
<configuration> <location path="OldPage.html"> <system.webServer> <httpRedirect enabled="true" destination="NewPage.html" exactDestination="true" httpResponseStatus="Permanent" /> </system.webServer> </location> </configuration>
Importing htaccess rules into Web.Config
- Access IIS then access the site in question.
- Acess the URL Rewrite Icon.
- On the right side click Import.
- Select your .htaccess file or text file in question to import.
# BEGIN WordPress #Options +Followsymlinks RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [NC,L] # END WordPress
- Click confirm.
Please Note: In some cases you may need to tweak the rules for formatting which it will give an error indication.
End Result of the web.config file:
<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <rewrite> <rules> <rule name="wordpress" patternSyntax="Wildcard"> <match url="*" /> <conditions> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> </conditions> <action type="Rewrite" url="index.php" /> </rule> </rules> </rewrite> </system.webServer> </configuration>