I'm trying to get some remote images to appear as if it is hosted locally. I can get this to work fine, but in the logs it is still showing as a 302 / 301
The rules is basically: RewriteRule ^local\.jpg$ http://www.example.com/remote.jpg [R=301]
Doesn't seem that [R=200] works.
As it's my server I assume there is a way to do this.
Thanks, Chris
not2easy
10:30 pm on Oct 17, 2015 (gmt 0)
What is the RewriteCond for that rule? Have you checked the headers to see right where that 302 is coming in?
lucy24
10:41 pm on Oct 17, 2015 (gmt 0)
Can I make a redirect show a 200 code
In a word: no. The essence of a redirect is that you're telling the browser to make a new request to suchandsuch location.
Doesn't seem that [R=200] works.
I'm surprised it didn't crash the server. OK, that's an exaggeration. It is true that you can attach a non-300 code to the R= flag ... but only within reason. You can return a 400-class or 500-class response-- but then the target, if any, is lost.
What you can do is rewrite-- not redirect-- to serve content from a different location. In this form, the target begins in / and the flag is [L].
As it's my server I assume there is a way to do this.
Good, because serving content from a different hostname cannot be done in htaccess, only in config. You'll need to read up on the [P] flag. In fact the Apache docs address precisely your situation: images that physically live elsewhere, but you want it to look as if they're local.
Here's the 2.2 version: [httpd.apache.org...] The 2.4 version is identical except that the docs add a "Performance Warning" which presumably applied to 2.2 as well; they just didn't think to mention it.
You will, of course, not write a rule containing the element "/(.*)" unless you want the rule to be inefficient at best, or to have unintended consequences at worst. ("Unintended consequences" is Apache for "the world as we know it may come to an end".)
whitespace
12:00 am on Oct 18, 2015 (gmt 0)
What about an Alias [httpd.apache.org] or AliasMatch [httpd.apache.org] (mod_alias) in your server config instead?
lucy24
1:08 am on Oct 18, 2015 (gmt 0)
D'oh! I get so used to htaccess, I forget that mod_alias has that whole other set of functions.
Did the images formerly exist at the location implied by their URL, or is this a new site where nothing had any earlier existence? This may turn out not to make any difference, but it can't hurt to amass all possible information.
Also: when you said "remote images" I assumed "images living on a different server". Did you actually mean "same server, but outside this domain's default directory structure"? This one probably does make a difference.
Pay attention to execution order, to ensure that your images haven't been intercepted by mod_rewrite before mod_alias gets a chance to do its stuff.
surfgatinho
3:06 pm on Oct 18, 2015 (gmt 0)
Many thanks for the swift responses. I'll start reading the docs - might take me a day or two to digest and apply the info though!
The images are on a third party server. Basically I would like them to appear for all intents and purposes that they are on my website, but due to legal issues i don't want them on my server. Nothing dubious or immoral, I just need to protect myself from any third party liabilities.
whitespace
4:19 pm on Oct 18, 2015 (gmt 0)
The images are on a third party server.
Ah ok, you won't be able to use mod_alias (Alias / AliasMatch) in that case.
surfgatinho
9:09 am on Oct 20, 2015 (gmt 0)
Many thanks. Seem to have it working using the [P] flag. Had to turn on mod_proxy and mod_proxy_http along with tweaking the proxy allow / deny settings in the httpd.conf file.
tangor
9:43 am on Oct 20, 2015 (gmt 0)
Nothing dubious or immoral, I just need to protect myself from any third party liabilities.
Odd man out: Why? Apparently there are liabilities. I bring this up only to warn others that serving third party images as if they originate on your site is a lie... unless there is a contractual agreement to do so. Just because you can does not mean YOU CAN.
This kind of stuff cannot be hidden. It will be found. Keep that in mind.