Page by Page redirects in IIS for .asp, .html, .pdf, etc.
I’ve gone over adding a domain wide 301 redirects before in IIS to fix the www and non-www canonicalization issues, but the same issue also comes up for individual pages as well.
For an old page that is a .asp page adding the following code to the top of the page will perform the redirect. You can get rid of all other content on the page, as no browser or crawler will see it once they receive the 301 redirect.
<%@ Language=VBScript %>
<%
Response.Status="301 Moved Permanently"
Response.AddHeader "Location", " http://www.example.com/newpage.asp"
Response.End
%>
That only works for pages with extensions set up to parse the asp code. A lot of times, specially on shared hosting environments, other pages with static extensions such as .html, .pdf, .txt, etc will not execute the code. With a little trickery we can accomplish that as well.
Let’s say you have the old file that you want to 301 redirect, for example:
www.example.com/old_directory/oldpage.html
Which you’d like to redirect to the new location:
www.example.com/new_directory/newpage.html
If you were to add the above code to newpage.html nothing would happen, or even the code would show up as text on the page, as more than likely your server is not set up to execute the code. Here’s what you can do:
- Delete (or rename it to save it for a rollback) the oldpage.html file located in /old_directory/
- Create a new sub-folder (note: it’s a folder not a file) under /old_directory/ with the same name as the file you just deleted, in this example you’d create a sub-folder named ‘oldpage.html’ like /old_directory/oldpage.html/.
- In that folder you just created you will then create a default asp file to execute the redirect code, this can be different depending on how your site is set up, but generally it is default.asp, such as you now have a page located at /old_directory/oldpage.html/default.asp
- in that default.asp you will want to add the following code:
<%@ Language=VBScript %>
<%
Response.Status="301 Moved Permanently"
Response.AddHeader "Location", " http://www.example.com/new_directory/newpage.html"
Response.End
%>
That’s it you’re done. Now when someone or a crawler visits the old page, they will be 301 redirected to the new location. As stated before this also works for other static pages such as .PDF, .DOC, .TXT, .HTM , etc.
For example if you visit this page, which appears like it was a PDF:
www.hvac-direct.com/pdfs/oldfile.pdf
You should be automatically redirected to a new html file in a folder:
http://www.hvac-direct.com/html-version/
To see that is actually a 301 redirect view the response on the oyoy.eu tools. Note: that it’s a double redirect because of the lack of trailing slash on that server set-up, but it’s better than returning a 404!
If you liked this post please buy me a beer. Thanks.
On February 2nd, 2008, g1smd said:

