大网数据广告活动
网站运营与故障问答

How can I do 301 instead of 302 redirects when there are multiple domains sharing the same site hosted in IIS?

· 52CE 我爱测

I have several domains sharing the same site and use a default.asp as the default file where I test the SERVER_NAME variable and use Response.Redirect to redirect to the home page of the site requested. The problem is that this t…

问题原文

原帖链接 · user1780087 · CC BY-SA 4.0

I have several domains sharing the same site and use a default.asp as the default file where I test the SERVER_NAME variable and use Response.Redirect to redirect to the home page of the site requested.

The problem is that this type of redirect causes 302 status code. Even when having a rewrite section in my web.config where I created one redirect rule for each site with redirectType="Permanent", the status code 302 is returned.

That is bad for ranking pages in Google's index. Does somebody know a way to solve that problem and return 301 status code?

已采纳解答

原帖链接 · Trebor · CC BY-SA 4.0

I'm assuming you're using classic ASP (not .NET) since you refer to default.asp and not default.aspx.

Instead of using response.redirect and counting on your web config file to change from a 302 to a 301, try adding the 301 to the response object before redirecting the user.

<%
Response.Status="301 Moved Permanently"
Response.AddHeader "Location", "new_page_name.asp"
%> 

If you're actually using .NET, then use the following:

<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location","http://example.com");
}
</script>

See https://docs.microsoft.com/en-us/previous-versions/iis/6.0-sdk/ms525844(v=vs.90) for more information.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2021-01-24)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。