繁体   English   中英

使用URL重写从asp.net中的子域提供静态内容

[英]Serving static content from a subdomain in asp.net using URL Rewrite

搜索了几个小时无济于事,所以这里去了...

我有一个域每天收到的点击量很高,有人问我是否可以从子域中提供静态内容。 由于该站点相当广泛且已经编写,因此我想知道是否有任何方法可以使用URL重写进行更改:

www.example.com/image.gif

static.example.com/image.gif

我有一个使用301重定向的解决方案,但是据我了解,这会适得其反,因为每个图像必须提出2个请求。 我真的不希望浏览所有的aspx页面和CSS来对新的URL进行硬编码,因为它将导致进一步的问题-网站的某些部分仍在开发中,静态内容可能随时更改。 我尝试使用重写(而不是重定向)来更改网址,但结果如下:

http://www.example.com/http://static.example.com/image.gif

您将如何实现? 我具有对dns和服务器(win 2008r2 / IIS 7.5)的完全访问权限,因此,如果URL重写不能解决问题,则可以进行任何更改。

提前致谢

汤姆

您可以使用HttpModule完成此操作。 下面的示例捕获对gif文件的所有请求,并将绝对路径更改为指向承载静态内容的备用站点。 在HttpModule中尝试以下方法:

    private void Application_BeginRequest(Object source, EventArgs e)
    {
        // Create HttpApplication and HttpContext objects to access
        // request and response properties.
        HttpApplication application = (HttpApplication)source;
        HttpContext context = application.Context;
        string filePath = context.Request.FilePath;
        string fullPath = context.Request.Url.AbsoluteUri;
        string fileExtension = VirtualPathUtility.GetExtension(filePath);
        if (fileExtension.Equals(".gif"))
        {
            context.Response.ContentType = "image/gif";
            context.Response.Redirect(fullPath.Replace("www.example.com", "static.example.com"));
        }
    }

有关HttpModule的更多信息,请访问: http : //msdn.microsoft.com/zh-cn/library/ms227673.aspx

我测试了上面的代码-它可以工作。 我希望这有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM