I have a php website hosted in a LAMP environment.
Many pages contain references to somedomain.com
(such as images src). If somedomain.com should become unreachable, my site would go to timeout (and error 500).
Is there a way to block all somedomain.com request of my pages, let's say redirecting them to localhost using .htaccess?
Asked
Active
Viewed 76 times
0

jasmines
- 135
- 12
-
I wouldn't know how to do this with a `.htaccess` file. But in php it's quite easy. Just request headers of `somedomain.com` and use if / else statement on 404 returned. See [this](http://stackoverflow.com/questions/2280394/how-can-i-check-if-a-url-exists-via-php) question for more info. – icecub Sep 08 '15 at 14:42
-
Its imposible because client(browser) call somedomain.com when load these images. Your .htaccess can manage connections only for yours server. This problem have solution - use proxy script on your server. – Aleksey Krivtsov Sep 08 '15 at 15:03
1 Answers
0
No, it's not possible because these resources are loaded by browser, e.g. for image:
<img src="somedomain.com/img/someimg.png" />
A possible way is to proxify these resources with a PHP script :
<img src="myproxy.php?url=http://somedomain.com/img/someimg.png" />
Which it's looks like :
<?php
$content = file_get_contents(@$_GET['url']);
/*
* There is an error
*/
if (!$content) {
$content = file_get_contents('alternate_local_img.png');
}
header('Content-Type: image/png');
echo $content;
It's can be improved with extension/mime type detection.

Thomas Champion
- 328
- 3
- 7