10-03
12

IIS Redirects - 301 , 302

Microsoft's Internet Information Server ( MS IIS ) is very different from Apache, and you need to handle redirects on it differently.

Although you can use the popular FrontPage web development software with Apache + FP extensions, most people who use FrontPage prefer to use IIS. Also, Microsoft's .NET and ASP development platforms usually work best on MS IIS (no surprise there).

Definitions for Terms used in this Article

Detailed Information
Overview
What you are trying to accomplish here is to have one resource (either a page or an entire site) redirect a visitor to a completely different page or site, and while doing so tell the visitor's browser that the redirect is either permanent (301) or temporary (302).

Therefore you need to do three things:

Have 2 resources - one source page or website, and one destination page or website.
When an attempt to access the source resource is made, IIS transfers the visitor to the destination instead.
During the transfer, IIS reports to the visitor that a redirect is happening and it's either temporary or permanent.
The good news is that IIS has always supported this, and you can use it's control panel to make the redirect.

Administrator Mode - Redirecting a Domain
If you can log into the Windows 2000 (or higher) server and access the desktop, then choose:

Start > Programs > Administrative Tools > Internet Services Manager

Now choose the server running the site you want to forward. Remember you need 2 sites - one to forward FROM and one to forward TO. These can be on the same, or separate servers.

Right click on the site you want to redirect FROM and choose Properties > Home Directory

You will see the following:

 


The default is the first choice, "A directory located on this computer". Change it to "A redirection to a URL" and type in the new URL.

If you want it to be a 301, then be sure to check ON the choice for "A permanent redirection for this resource". If you want it to be a 302, leave the choice checked OFF.

Administrator Mode - Redirecting an Individual Page
If you can log into the Windows 2000 (or higher) server and access the desktop, then choose:

Start > Programs > Administrative Tools > Internet Services Manager

Now choose the server running the site you want to forward. Choose the site with the webpage you want to forward in it, then right click on the it and choose "Properties".

You will see the following:

 


The default is the first choice, "The designated file". Change it to "A redirection to a URL" and type in the new URL.

If you want it to be a 301, then be sure to check ON the choice for "A permanent redirection for this resource". If you want it to be a 302, leave the choice checked OFF.

If you don't control the IIS server, ask the admin to do the above. Done.

Passing on Variables or a Query String During IIS Redirects
Let's say that you want to pass on some variables, for example, you wanted to redirect an ASP site that accepted arguments for some pages and pass those same arguments on to the same pages at the new site.

In this case, in the "Redirect to:" box, enter the domain you wish to move to (no trailing slash), plus $S$Q .

For example:

http://www.newdomain.com$S$Q

Next, check the options that state the client will be sent to "The exact URL entered above", as well as "A permanent redirection for this resource" (if you want it to be a 301). Done.

What does this $S$Q do? These are tags that IIS will automatically replace - $S will be replaced with the subdirectory location (such as /shopping/cart.aspx) and $Q will be replaced with the querystring (such as ?id=Blue).

Server Variable Function Example
$P Passes parameters that were passed to the URL to the new URL. If the request contains parameters such as http://www.oldsite.com/cart.asp?id=Blue , then $P would represent all the values after the question mark in the URL, example $P would equal id=Blue (no question mark).
$Q Passes the parameters including the question mark. This is the same as $P but includes the question mark or query string. So $P would equal ?id=Blue
$S Passes the matching suffix of the URL to the new URL. If the request is for http://www.oldsite.com/shopping/cart.asp, then $S represents /cart.asp. If the request was for http://www.oldsite.com/shopping then the value of $S would be /shopping
$V Removes the server name from the original request. If the request is for http://www.oldsite.com/shopping/cart.asp then $V would contain everything after the server name, eg: /shopping/cart.asp.
* Wildcard symbol used for replacement. Let's say you want to redirect all requests for html pages to a single asp page - you could do so in the following way: *;*.htm;page.asp

 
This works for both Site Redirects and Individual Page Redirects.

Common Scenarios
I Just Want To Switch Domains!
This is actually pretty straightforward. You can tweak things to get exactly what you need with the variables above, but the following will work for the most common setups:

http://www.newdomain.com$V$Q

Do NOT add a trailing slash "/" at the end of the domain name!

Make sure you check:

The exact URL entered above
A permanent redirection for this resource
Done! This will send everything after the old domain name to the new domain name, including variables. You need to do is make sure that the new website is set up exactly the same as the old one and that all you are doing is changing domain names.

Non-WWW to WWW Redirect for IIS
When you set up a site in IIS, usually you set up the site with a domain name and add the domain name with both versions, www and non-www, to the host headers for the account. This creates a potential duplication issue, though.

The better way to do this is to actually create 2 accounts - one with the www version and one without. Then you put the website in your preferred version and a single page in the other. Normally, you would then treat this as if you were switching domains (above), with the 2 domains being the www and the non-www versions.

Another method, for those that don't have direct access to the control panel (common with many ISP's/ hosts), is to put your entire website in the www account and then a single page in the non-www account. Then you would put the following code in the default page in the account with the single page (i.e. the one you are redirecting):

< %@ Language=VBScript %>
< %
Response.Status="301 Moved Permanently"
Response.AddHeader "Location", http://www.example.com
%>

What about Parameters?
If you do a non-www to www redirect for MS Internet Information Services (IIS) and have parameters that need to be passed on, then do the following (Thanks to Chris Hirst for testing this):

Do the above non-WWW to WWW redirect
Make double sure that you do NOT have a trailing slash at the end of the domain (http://www.newdomain.com$V$Q, NOThttp://www.newdomain.com/$V$Q)
As with all these redirects, make sure that you check "The exact URL entered above"
Don't Have Administrator or Desktop Access?
If possible, ask the Administrator for the site to perform the above steps. If you can't, you need to use ASP or .NET scripting on-page to do redirects.

Conclusion
IIS is a powerful and easy to use web hosting platform. If you have access to the Admin panel you can accomplish basic tasks (like simple redirection) easily and quickly. If you don't have access (and don't want to switch hosts) then you will have to use redirection scripting to accomplish this. You'll learn more about that in the Redirects Using On-Page Scripting and Headers section.

Definitions for Terms used in this Article

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/canduecho/archive/2009/07/10/4336764.aspx



文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: IIS 301 重定向 302
相关日志:
评论: 0 | 引用: 0 | 查看次数: 449
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭