Can apache rewrite to external url?

Asked

Viewed 632 times

10

I have an application running on local. At the same time I have the application that is already running in production - namely a social network.

I have photos of many users in this system that is in production, but as it is not feasible to put them in the repository of git, or copy them all to the computer, I wonder if there is any way to get apache to rewrite the url of that local application, when it does not find the image (in place), to the url of the application that is in production.

It would be something like this:

local/public/teste.jpg => https//producao.com.br/public/teste.jpg
  • Did you make it? The report I gave helped?

3 answers

4


You can use the P flag on-mode mod_rewrite rule to replace URL with mod_proxy:

RewriteEngine on
RewriteRule ^minhaPasta/$ http://outro.exemplo.com.br/outraPasta/ [P]

Now when the customer orders /minhaPasta/ on your server, it requested http://outro.exemplo.com.br/outraPasta/ and send that reply to the customer.

3

You can use the module mod_rewrite.

Add the block below inside <VirtualHost><Directory> in your configuration file or .htaccess if your website is allowed to change Apache settings.

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule "^public/(.*)" "https://www.producao.com.br/public/$1" [R,L]
</IfModule>

You have to activate the module if it is not yet active.

Example with Linux:

a2enmod rewrite
service apache2 restart

Service has to be restarted when modules are enabled/disabled. reloadis not sufficient in this case.

2

You can even only redirect to the server without rewriting, but you can face blockages with the Referer: or difficulties with using the canvas images for example, because of CORS.

The recommended is to use a webproxy (using CURL for example) combined with Apache (if you happen to use technologies canvas, many "Html5" plugins make use of this).

Assuming the photos are all from a single domain, it would look something like:

<?php
if (empty($_GET['path']) === false) {
     echo 'Caminho não definido';
     exit;
}

$path = $_GET['path'];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://site-externo.com/' . $path);
curl_setopt($ch, CURLOPT_HEADER, false);

//Envia o user agente do navegador atual
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);

//Força retornar binario
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);

//Pega os dados
$data = curl_exec($ch);

//Fecha o curl
curl_close($ch);

$ch = NULL;

$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if($data === false)
{
    http_response_code(404);
    echo 'Curl error: ' . curl_error($ch);
} elseif ($httpcode !== 200) {
    http_response_code($httpcode);
} else {
    $finfo = new finfo(FILEINFO_MIME_TYPE);
    header('Content-Type: ' . $finfo->buffer($data));
    echo $data;
}

And . htaccess should look like this:

RewriteEngine On
RewriteRule "^local/public/(.*)" proxy.php?path=$1

note: I don’t use Curl very much, it may be that something is missing, let me know if something fails

  • Incorrect information. It is possible to rewrite Urls with apache and redirect the request to any URL.

  • @Erico not wanting to offend you, I’ve come to assume that "maybe" does not know the R flag of mod_rewrite. Actually what you did in your reply was a redirect, because two different servers do not talk to mod_rewrite, what he wants is to rewrite and not redirect. In case he wants localhost to access an external server, it’s not the same server. I hope my explanation is clear :)

  • William, yes, the redirect can be done to an external server, you can test the redirect I posted. He doesn’t want to literally rewrite the URL, he wants to load the production server images to the local server if they don’t exist locally, without making any changes to the code.

  • William, I’m not saying your answer is incorrect, I’m saying the information related to apache is. It’s running locally, so the requisitions will come from the same place. If there is any restriction with CORS, which I find unlikely, it will not apply here. It just wants to load the images from the production site, rss.

  • In fact you may have even intended not to say, but your text "Incorrect information." gives to understand exactly this :) - Ok, see the edition, there are problems with CORS and HOTLINKS that are blocked sometimes. Using webproxy helps work ;) @Erico

  • @Guilhermenascimento once again: This is the kind of question, where I already got an answer by myself (at the base of the fuçometro). I did it this way and it worked: RewriteRule ^(.*)\.(jpg|bmp|png)$ https://www.site.com.br/$1.$2

  • @Wallacemaxters just did not understand, you offered the reward today. Solved today even the problem?

  • No, @Guilhermenascimento. I have a lot of points and wanted to see that question answered :). In addition to what will benefit someone ;). I’m about to go on vacation and I’m slapping old questions

  • 1

    @Wallacemaxters anyway, consider the answer here as alternative when using technologies commonly called Html5 and face CORS problems or have problems with Hotlinks, maybe it is useful even to cache headers ;)

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.