Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite to image server

         

63bus

6:44 am on Mar 30, 2007 (gmt 0)

10+ Year Member



I recently set up an images.example.com subdomain on my dedicated server, using a spare IP. So that I don't have to hardcode images.example.com and the full path into all of the pages, I want to automatically redirect all .jpg and .gif images to this subdomain so that anything like:

http://www.example.com/dir1/1.jpg
http://www.example.com/dir2/2.jpg
http://www.example.com/dir3/subdir1/3.jpg
http://www.example.com/dir4/subdir1/subsubdir1/4.jpg

becomes

http://images.example.com/dir1/1.jpg
http://images.example.com/dir2/2.jpg
http://images.example.com/dir3/subdir1/3.jpg
http://images.example.com/dir4/subdir1/subsubdir1/4.jpg

This should include anything that is example.com instead of www.example.com too.

What is the syntax to do this using RewriteRule?

[edited by: encyclo at 7:00 pm (utc) on Mar. 30, 2007]
[edit reason] switched to example.com [/edit]

jdMorgan

1:35 pm on Mar 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd suggest using the [P] flag of RewriteRule to reverse-proxy the image requests through to the image server. Doing this instead of a redirect will eliminate the need for the client (browser or robot) to make a second HTTP request.

In general, the syntax would be something like:


RewriteRule ^([^.]+)\.jpg$ http://images.example.com/$1.jpg [P]

However, this will reverse-proxy *all* requests for images to the back-end image server. You may need to add conditions (using RewriteCond) to explicitly include or exclude certain paths from the rewrite. Or you may need to modify it to also rewrite requests for .gif and .png files as well -- It's impossible to tell unless you specify exactly and completely how RewriteRule should identify URL-paths which should and should not be rewritten.

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

In addition to the info included on and linked-to by the above-cited pages, see also Apache mod_proxy for information about forward versus reverse proxies -- You do NOT need a forward proxy for this application, and should NOT enable forward proxy functions unless needed for other services.

Jim

63bus

6:25 pm on Mar 30, 2007 (gmt 0)

10+ Year Member



Thank you, that clarifies things for me based on some other examples I was looking at and the reading I was doing.

Yes, I do want to redirect all of the images.

My image server is lighttpd and not Apache so I'm assuming I do not want to or cannot use a proxy. Is this correct?

jdMorgan

3:07 am on Mar 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Many Webmasters reverse-proxy to back-end servers running TomCat, and a bunch of other stuff. As long as the back-end server speaks "HTTP", Apache doesn't care...

See Apache mod_proxy, and read the description of a reverse proxy; it would save your visitors a lot of wasted time waiting for redirects: If you redirect every image, then every user will have to wait for two HTTP request/response transactions per image, instead of only one. This won't be true once their browser caches the images (assuming you allow it), but your site may seem very slow to many first-time visitors...

Jim

63bus

5:15 pm on Apr 3, 2007 (gmt 0)

10+ Year Member



Thanks, I got everything working as a reverse proxy.

Unfortunately it seems like using mod_proxy and the RewriteRule command results in a higher overall load on my server at all times.

jdMorgan

5:22 pm on Apr 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What's loading the server is the fact that requests and responses must pass through the front-end server to/from the back-end. While it doesn't have to do any of the work looking up the files and adding the HTTP response headers, all the data does have to pass through the front-end server. So, you've still got plenty of low-level driver activity for the proxied requests.

The alternative is setting up a separate domain or subdomain, such as images.example.com, and linking to the images there.

Jim

63bus

6:51 pm on Apr 3, 2007 (gmt 0)

10+ Year Member



Yes, I have an images.example.com sub-domain that goes directly to my lighttpd image server. I wanted to automatically redirect anything that might come through Apache without having to hard code all my HTML and PHP pages to use images.example.com.

Due to the design of my site, the images are laid out over a number of subdirectories such as:

http://www.example.com/dir2/2.jpg
http://www.example.com/dir3/subdir1/3.jpg
http://www.example.com/dir4/subdir1/subsubdir1/4.jpg

with all the pages in subdirectories using <img src="1.jpg"> or <img src="xyz/1.jpg">. There is no easy way to globally update all my pages to point to the images.example.com sub-domain.

Or is there? Suggestions welcome.

jdMorgan

7:25 pm on Apr 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Other than the ".jpg" file ending, are there any other indicators in the URL that the request should be redirected? Or is it the case that *all* images should be redirected?

I assume that you have linked to the images at their new subdomain; Otherwise, you'll still tie up you main server redirecting all of these image requests.

Based on the URL, how can you tell requests that should be redirected from those that should not, or are all image requests to be redirected?

Jim

63bus

9:39 pm on Apr 3, 2007 (gmt 0)

10+ Year Member



I am trying to redirect all images, JPG and GIF. Every image on my entire site should be served from the images sub-domain.

Right now, as a test, I was simply using the command you posted to do JPG files only but was unsatisfied with the resultant server load increase.

>I assume that you have linked to the images at their new subdomain; Otherwise, you'll still tie up you main server redirecting all of these image requests.
>
I'm not totally sure what you mean by this. I have not updated the pages to use <img src="images.example.com/a/b/c/1.jpg"> from <img src="c/1.jpg">, I was relying on the RewriteRule to rewrite 1.jpg to be images.myexample.com/a/b/c/1.jpg for me.

jdMorgan

12:04 am on Apr 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not totally sure what you mean by this. I have not updated the pages to use <img src="images.example.com/a/b/c/1.jpg"> from <img src="c/1.jpg">, I was relying on the RewriteRule to rewrite 1.jpg to be images.myexample.com/a/b/c/1.jpg for me.

In that case, you will still see a significant load on the main server and users will see slow image loading, because their browsers will have to request each image twice -- once from the (linked-to) main domain server, and then again from the (redirected-to) image server. That will continue to be the case until you change your on-page links (You might want to try searching for multi-file search-and-replace tools, many free).


RewriteRule ^([^.]+\.(gif¦jpg))$ http://images.example.com/$1 [R=301,L]

Replace the broken pipe "¦" character above with a solid pipe before use; Posting on this forum modifies the pipe character.

Jim

[edited by: jdMorgan at 12:28 am (utc) on April 4, 2007]

63bus

1:48 am on Apr 4, 2007 (gmt 0)

10+ Year Member



Ok, I understand now.

Apparently I misunderstood your explanation of [P] above then. I thought using [P] would cause it to do a reverse proxy and only cause a single request.

jdMorgan

1:58 am on Apr 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When using [P], you get one request on the internet, and another on the intranet (if your back-end server is in your network). When using [R], you get two requests on the internet.

Jim

63bus

2:52 am on Apr 4, 2007 (gmt 0)

10+ Year Member



I have both web servers on the same machine.

jdMorgan

1:04 pm on Apr 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Nevertheless, the communication between the front-end server and the back-end server is accomplished using the HTTP protocol. (I wish we had a good way of posting pictures/drawings here, it would make explaining this *so* much easier...)

You should view the proxy and redirect methods as "quick-fixes" which ameliorate, but do not cure, the underlying problem of having "obsolete" image links. While they accomplish the goal, they do so at the cost of a significant extra load on the server and/or a delay and extra work for the client.

Jim

63bus

12:53 am on Apr 5, 2007 (gmt 0)

10+ Year Member



Great, thank you for explaining everything.