How to Generate web.config files for IIS (Redirection & Error Page control)
An IIS web.config file is an XML file containing rules for a particular site (or directory) on your web server. It is similar to a .htaccess file in Apache.
This file may contain rules setting 404, 403, etc. error pages for your site as well as redirection rules for older URLs.
Any valid web.config rules may be added to this Content Type.
Please note that your web server must be running Microsoft IIS 7+ and your web server administrator should be consulted before carrying out this task.
If you have an existing web.config file at the publish location, this will overwrite it! You should be sure to copy existing rules into Site Manager before publishing!
Create The Web.Config Generate File Navigation Object
Create a new Navigation Object which will generate the web.config file. This will generate a file called web.config in the same directory as the content, within the published directory. Use the following options/settings:
File Name | web |
---|---|
Append Content ID | Do not check this option |
File Extension | config |
Base directory | Leave this blank |
Append Directory | Do not check this option |
Formatter | text/web_config |
Media File | Do not select a piece of Media |
Create the web.config Content Type
This will allow users to enter the XML for the web.config file. Use the following as the General Content Type Information.
Content Type Name | Create web.config files |
---|---|
Content Type Description | Outputs a web.config which may be used to set various rules in IIS |
Minimum User Level | Administrator |
Enable Direct Edit | No (Unchecked) |
eForm | No (Unchecked) |
Default Workflow | None |
It should have the following element:
Name | Type | Compulsory | Max. Size | Notes |
---|---|---|---|---|
web.config XML | Plain Text | Yes | 10000 characters | Enter the XML code for the web.config file |
Content Layouts
Create a text/html layout with the following parameters. Note: If your channel uses a different default layout then you should use that here instead!
Replace the 'xxx' in the T4 Tag with the ID of the navigation object you've created above.
Content Layout Name | text/html |
---|---|
File Extension | Default |
Syntax type | HTML/XML |
Content Layout Processor | T4 Standard Content Layout Processor |
Content Layout Code | <t4 type="navigation" id="xxx"/> |
Create a text/web_config layout with the following paramenters:
Content Layout Name | text/web_config |
---|---|
File Extension | Default |
Syntax type | HTML/XML |
Content Layout Processor | T4 Standard Content Layout Processor |
Content Layout Code | <t4 type="content" output="normal" modifiers="" name="web.config XML" /> |
Creating a web.config (examples)
Typically (particularly if you're setting error pages) you'll want to have your web.config at the root of your site. For this reason, you should enable the content item on the Home section for your site and add content to that section.
Here are some examples of the code you'd add in the web.config XML field when creating content.
Redirects and Error Pages
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<httpErrors>
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" prefixLanguageFilePath="" path="/404/index.html" responseMode="ExecuteURL" />
</httpErrors>
<rewrite>
<rules>
<rule name="New Contact Us Page Redirect" stopProcessing="true">
<match url="^(about/us.*)" />
<action type="Redirect" url="http://www.example.com/about_us/" />
</rule>
<rule name="New About Page Redirect" stopProcessing="true">
<match url="^(contact/us.*)" />
<action type="Redirect" url="http://www.example.com/contact_us/" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
This example will set a 404 page as well as two redirects. Note the following:
- The error page path has a leading slash in it
- The redirect match urls do not have leading slashes
Just Error Pages
Here is an example setting two error pages, one for 404 errors and one for 401 errors.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<httpErrors>
<remove statusCode="404" subStatusCode="-1" />
<remove statusCode="401" subStatusCode="-1" />
<error statusCode="404" prefixLanguageFilePath="" path="/404/index.html" responseMode="ExecuteURL" />
<error statusCode="401" prefixLanguageFilePath="" path="/401/index.html" responseMode="ExecuteURL" />
</httpErrors>
</system.webServer>
</configuration>
Just Redirects
Here is an example setting three URL redirects
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="New Contact Us Page Redirect" stopProcessing="true">
<match url="^(about/us.*)" />
<action type="Redirect" url="http://www.example.com/about_us/" />
</rule>
<rule name="New About Page Redirect" stopProcessing="true">
<match url="^(contact/us.*)" />
<action type="Redirect" url="http://www.example.com/contact_us/" />
</rule>
<rule name="Alumni Vanity URL" stopProcessing="true">
<match url="^(alumni.*)" />
<action type="Redirect" url="http://www.example.com/information/general/alumni/" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>