This article takes you through a simple scenario for when you want to map a specific set of website urls to a new set of urls. If you are restructuring your site this is the place to start.
When you take over a site you will often find that the previous developers didn't pay much attention to the file names and the general url structure. You could also find that after a business restructure certain pages are no longer required. Perhaps a page was spelled incorrectly and got missed.
In any of these situations you don’t want to just cut the old pages loose and start again, its important that you set up redirects for the old pages to the new pages.
Why would you care about redirecting urls?
Well if you’re a professional web developer you shouldn’t really be asking this but let’s look at the reasons:
- Url’s exist forever. When you publish a url on the web it stops being in your control and should be maintained for as long as the site exists. If somebody has bookmarked them and they come back they should be able to get to the content or the equivalent content through their bookmark. I’ve seen lots of big name sites break this rule and its very naughty of them.
- Google doesn’t update straight away. If you relaunch as site then it can take a few more visits from the Googlebot to realise your old pages are gone and aren’t coming back. Don’t annoy your visitors by letting them click on your site in the Google SERPs (Search Engine Results Page) and be greeted with a 404 not found.
- You are shooting yourself in the foot SEO-wise. All of the existing links pointing into the site are now worthless if they point at an error page.
The basic scenario
This article covers mapping a simple set of urls to a new set of urls, in a 1 to 1 page mapping. If your site has thousands of urls, or a complex querystring based CMS then you will need to keep on looking for more advanced url rewriting tutorials as this simple one isnt going to cut it. I have written about url rewriting before, you might find something useful in my previous blog posts.
The times when you would want to apply this:
- You have replaced a simple static, brochure site with a new one and fixed the urls up, eg:
FROM: /prod_w.html TO: /products/bluewidget.html - You have replaced a site that uses the same urls but a different extension:
FROM: /products/bluewidget.php TO: /products/bluewidget.aspx - You need to fix a spelling mistake that snuck into production:
FROM: /products/bluewodget.html TO: /products/bluewidget.html
Prerequisites
First off, as the article title implies, the technique works on IIS7 and above, with the Microsoft IIS Url Rewrite module installed. You can install it by using the Microsoft Web Platform Installer at this link:
Or by directly downloading it here:
Url Rewrite Module to the rescue with rewrite maps
Open up your web.config file and scroll down to the <system.webServer> section.
We will be placing the code as its own item inside this tag.
Put this code into the web.config:
<rewrite> <rewriteMaps> <rewriteMap name="StaticRewrites" defaultValue=""> <add key="/oldurl.aspx" value="/newurl.aspx" /> </rewriteMap> </rewriteMaps> <rules> <rule name="RewriteMap Rule"> <match url=".*" /> <conditions> <add input="{StaticRewrites:{REQUEST_URI}}" pattern="(.+)" /> </conditions> <action type="Redirect" url="{C:1}" /> </rule> </rules> </rewrite>
The important line is the one on line 4, the <add> tag.
You can add as many of these rows as you like if you want to redirect several pages:
<add key="/oldurl.aspx" value="/newurl.aspx" />
Save the web.config file out and upload it to your server.
This will create a 301 redirect for each url that appears in the “key” and redirects to the “value”. A 301 redirect is a search engine friendly, permanent redirect and its the one that you want so Google and everyone else can understand where the new content is.
Testing our work
Now we can test out the urls by visiting any of the header checkers online and dropping in some test urls.
A header checker is a script offered by many websites that will test a url for you and return the HTTP headers back to you instead of the webpage itself. This lets you verify from a third party that the correct response is being returned. A random header checker site I just plucked out of Google is:
Type in the full url of one of your old pages, click Submit, and you should see something like this as a response:
This shows you that the old url is return a 301 redirect reply. The Location is the place its being sent to. Everything is setup correctly.
Conclusion
This guide has introduced you to the joys of using the IIS URL Rewrite module to quickly and easily set up 301 redirects on your site. You’ve learned that this technique is limited to simple static urls and if you are looking to redirect a complex pattern you will need to dig a little deeper. You have also learned some background as to why this is a good idea and how to implement it using the Static Map feature.
No comments :
Post a Comment