Serving static content from a subdomain in asp.net using URL Rewrite?

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")); } } 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.

– Tom Broad Feb 25 at 15:22 I noticed yesterday that it gives a 302 response for the original image request, and a 200 for redirected file (I setup a demo file in the redirected location, hence the 200). That should be fine, unless you want to avoid it for some reason. I'm not sure about the text/html issue.Is there any obvious difference between that image and the rest?

– Jonathan Nesbitt Feb 25 at 15:34 Tom, you should be able to set the content type explicitly. See my answer above - I added a line within the if block. – Jonathan Nesbitt Feb 25 at 15:39 One other thing: there is a better solution to change the domain that using Replace() on the fullPath string above.

If I get a chance later today, I will update the code with a more ideal solution. – Jonathan Nesbitt Feb 25 at 15:45 Yeah, I kind of came up with a similar solution regarding the content type and no, the image is no different! Must just be one of those weird bugs.

It turns out it's actually faster not to use a static domain in this case. Maybe the combination of 302 and response. Replace outweights any potential speed increase.

Shall I wait for another solution before I mark this as answered? – Tom Broad Feb 25 at 16:03.

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. 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.

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.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions