重定向的Default.aspx根虚拟目录 [英] Redirecting default.aspx to root virtual directory

查看:152
本文介绍了重定向的Default.aspx根虚拟目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个虚拟目录下IIS7下运行一个简单的ASP.NET 3.5应用程序。所以,我的应用程序的URL是如 http://example.com/app <​​/A>。我想301请求重定向到example.com/app/default.aspx~~V到example.com/app~~V更好的搜索引擎优化。我必须这样做,到$ C $重定向仅C,不受任何IIS设置。我无法通过本文中提到的code这样做的:

I have a simple ASP.NET 3.5 application running under IIS7 under a virtual directory. So the URL of my app is like http://example.com/app. I want to 301-redirect the request to example.com/app/default.aspx to example.com/app for better SEO. I have to do this redirect through code only, not by any IIS settings. I am unable to do so via code mentioned in this article:

http://www.4guysfromrolla.com/articles/072810-1.aspx

在code:

if (request.RawUrl.Equals("/default.aspx"))
{
newUrl = string.Format("{0}://{1}{2}",
                 request.Url.Scheme,
                 request.Url.Authority,
                 request.RawUrl.Remove(request.RawUrl.LastIndexOf("/default.aspx", StringComparison.OrdinalIgnoreCase)));

               context.Response.Status = "301 moved permanently";
               context.Response.AddHeader("Location", newUrl);
}

似乎进入一个无限循环,当应用程序是一个虚拟目录下。在request.RawUrl属性总是甚至301重定向造成无限循环后返回/default.aspx。我怎样才能解决这个问题?

seems to go into an infinite loop when the application is under a virtual directory. the request.RawUrl property always returns "/default.aspx" even after a 301 redirect causing the infinite loop. How can I fix this issue?

感谢,

阿西夫

推荐答案

以上code将正常工作,只要你没有一个子目录。据我所知,它在ASP.NET中的错误:在Request.RawUrl不应包含/default.aspx当URL不具有扩展名。我已经测试您的code和它没有一个子目录工作正常,但如果Default.aspx的是一个目录下,则Request.RawUrl对象无法摆脱的Default.aspx,因此无限循环。

The above code will work fine as long as you dont have a sub-directory. AFAIK, its a bug in ASP.NET: the Request.RawUrl should NOT contain "/default.aspx" when the URL does not have that extension. I have tested your code and it works fine without a sub directory, but if default.aspx is under a directory, the Request.RawUrl object fails to get rid of default.aspx and hence the infinite loop.

这篇关于重定向的Default.aspx根虚拟目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆