Forum Moderators: phranque
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]
In general, the syntax would be something like:
RewriteRule ^([^.]+)\.jpg$ http://images.example.com/$1.jpg [P]
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
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
The alternative is setting up a separate domain or subdomain, such as images.example.com, and linking to the images there.
Jim
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.
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
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.
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]
Jim
[edited by: jdMorgan at 12:28 am (utc) on April 4, 2007]
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