Here is an example code of how to do a redirect using a web.config file.
web.config 301 redirect
This example will redirect single pages to a new location.
Let say the new pages of your site have .html extensions and you want the new location to be its own directory
(IE. http://yourdomain.com/services.html will change to http://yourdomain.com/sales/).
- Open web.config in the directory where the old pages are located.
- Add code for the old location path and new destination:
<configuration>
<location path=”sales.html”>
<system.webServer>
<httpRedirect enabled=”true” destination=”http://yourdomain.com/sales” httpResponseStatus=”Permanent” />
</system.webServer>
</location>
<location path=”inventory.html”>
<system.webServer>
<httpRedirect enabled=”true” destination=”http://yourdomain.com/inventory” httpResponseStatus=”Permanent” />
</system.webServer>
</location>
</configuration>
You can add as many location paths as necessary.
The other example will redirect an entire directory to a new location.
For example, if you want http://yourdomain.com/dir-old/ redirected to http://domain.com/new-directory/ open web.config in /dir-old, then add the following line of code within the <system.webServer> section:
<httpRedirect enabled=”true” destination=”http://yourdomain.com/new-directory” httpResponseStatus=”Permanent” />