Can apache rewrite to external url?

10

I have an application running in 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 on this system that are in production, but since it is not feasible to put them in the git repository, or copy them all to the computer, I wonder if there is any way to do that Apache rewrites the url of that local application when it does not find the image (in place) for 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
    
asked by anonymous 18.09.2015 / 13:46

3 answers

4

You can use P flag in mode mod_rewrite rule to replace URL with mod_proxy :

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

Now when the client requests /minhaPasta/ on your server, it will request http://outro.exemplo.com.br/outraPasta/ and send that response to the client.

    
17.12.2015 / 18:15
3

You can use the mod_rewrite module.

Add the block below within <VirtualHost><Directory> in your configuration file or in .htaccess if your site 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 already active.

Example with Linux:

a2enmod rewrite
service apache2 restart

The service must be restarted when modules are enabled / disabled. reload is not sufficient in this case.

    
17.12.2015 / 17:59
2

You can even just redirect to the server without rewriting, but you may experience locks with Referer: or difficulties with using canvas images for example, because of CORS .

It is recommended to use a webproxy (using CURL for example) combined with Apache (if it is to use canvas technologies, 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 do not use curl much, something might be missing, please let me know if something goes wrong

    
17.12.2015 / 18:02