使用URL重写从asp.net中的子域提供静态内容 [英] Serving static content from a subdomain in asp.net using URL Rewrite

查看:60
本文介绍了使用URL重写从asp.net中的子域提供静态内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Searched for a few hours with no avail so here goes...

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

I have a domain which is receiving quite a few hits per day and have been asked if I can serve the static content from a subdomain. As the site is quite extensive and already written, I was wondering if there is any way I can use URL rewriting to change:

www.example.com/image.gif

www.example.com/image.gif

static.example.com/image.gif

static.example.com/image.gif

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

I have a solution which works using 301 redirects but from what I understand, this is counter productive as 2 requests will have to be made per image. I don't really want to go through all the aspx pages and css to hard code the new url as it will cause problems further down the line - some parts of the site are still being developed and static content could change at any time. I tried using rewrite (as opposed to redirect) to change the url but it came out something like:

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

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

How would you achieve this? I have full access to dns and the server (win 2008r2 / IIS 7.5) so can make any changes if url rewriting is not the answer.

预先感谢

汤姆.

推荐答案

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

You can accomplish this using an HttpModule. The following example captures all requests for gif files, and changes the absolute path to point to your alternate site that hosts the static content. Try this method inside of a 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/en-us/library/ms227673.aspx

For more about HttpModule, go here: http://msdn.microsoft.com/en-us/library/ms227673.aspx

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

I tested the above code - it works. I hope this helps.

这篇关于使用URL重写从asp.net中的子域提供静态内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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