.htaccess Redirect Media File Requests to a Different Domain/Server - apache

Simply speaking, I want to substitute one file path for another in the URI, but only for certain file types.
I have a load of image files (PNG, GIF and JPG) on one server and a wordpress installation on another server. I can't put them all on the same server at the moment (for reasons too complicated to go into).
So, when I get a request for a PNG, GIF or JPG file on e.g.
http://www.server1.com/images1/image1.png
I want to be able to divert this request to the same image, but on server 2, potentially in a different top level subfolder e.g. "allimages" such as:
http://www.server2.com/allimages/images1/image1.png
Then, say divert:
http://www.server1.com/images2/image2.png
to
http://www.server2.com/allimages/images2/image2.png
I tried to make a start with .htaccess (on SERVER1) but haven't got very far. I put a .htaccess file in the root of Server 1, with these lines in:
RewriteEngine On
RewriteCond %{REQUEST_URI} (\.png|\.jpg|\.gif|)$
RewriteRule ^(.*)$ http://www.server2.com/allimages/$1 [L,R=301]
But I know this isn't correct. Can anyone help? Many thanks!

Try with:
RewriteEngine On
RewriteRule ^(.+(?:\.png|\.jpg|\.gif))$ http://www.server2.com/allimages/$1 [L,R=301]

Related

.htaccess rewrite root to folder, yet block direct urls to same folder

I've been researching and trying for a week to accomplish this, but I haven't been able to find my solution. I'm sure it's possible, but I'm just not an expert in the depths of voodoo magic.
The setup:
An installation of MantisBT located in
mysite.com/mantisbt/currentver/mantis-1.3.19
When I perform an upgrade, I want to archive all old versions and old
database dumps to /mantisbt/oldversions/ to keep things tidy.
I also have other utilities and pages in various subdirectories, for
instance "mysite.com/utils"
The goal:
I want users to type in mysite.com/ (root) and have the URL rewritten
(transparently) to /mantisbt/currentver/mantis-1.3.19/ so that they
don't see my directory structure and it looks like the mantisbt app is
located in the root directory.
I also want protection from anyone trying to directly access a
subdirectory beginning with "/mantis". For instance, if
someone directly types mysite.com/mantisbt/currentver/mantis-1.3.19/
into their browser, I want them redirected back to the root directory
so they access the site from root just like everyone else.
I also need to allow my other subdirectories like mysite.com/utils to
be accessible if I type in the full path to them.
The biggest problem I've encountered is that Apache loops through your .htaccess file again every time the URL changes. So I get stuck in these rewrite loops. I've tried looking at every possible tool that Apache offers, but I'm seriously outgunned here. I could provide examples of what I've tried, they're obviously not correct, but ask me and I can post them.
You'd be far better off altering the DocumentRoot in your httpd.conf, and mapping in the utils directory, via a Location directive. It will be far cleaner, and Apache won't have to do some trickery with every request.
Anyway something along the following lines should work.
# Stop direct access
RewriteCond %{HTTP_REFERER} !mysite.com [NC]
RewriteRule /?mantisbt/currentver/mantis-1.3.19 / [NC,L,R=301]
# Internally Rewrite everything apart from /utils to /mantisbt/mantis-1.3.19/
RewriteCond %{HTTP_REFERER} !mysite.com [NC]
RewriteCond %{REQUEST_URI} !/utils [NC]
RewriteRule .* /mantisbt/currentver/mantis-1.3.19%{REQUEST_URI} [L]

mod_rewrite inserting full path to file

I need to create a rewrite to take traffic going to mp3/mp4 files in a specific subdirectory and then route them to a PHP file that tracks download stats etc before routing them to the actual file location since iTunes requires your podcast RSS contain actual media file extensions (.mp3, .mp4, etc)
I have created rewrites before with no problem but now I am running into an odd issue on this company's server.
My .htaccess located at www.company.com/companytools/podcasts
RewriteEngine on
RewriteRule ^/(.*).mp3$ /test.php?file=$1 [r=301,L]
Right now it is partially working it does act upon the mp3 file but ends up including the full path to test.php after the domain, so I end up with a 404 page looking for this URL:
www.company.com/www/internal/docs/companytools/podcasts/test.php?file=test
basically I need the path, but only the /companytools/podcasts part.
Any help is appreciated.
You may not need R=301 here to hide actual PHP handler.
Try this rule with RewriteBase:
RewriteEngine on
RewriteBase /companytools/podcasts/
RewriteRule ^(.+?)\.mp3$ test.php?file=$1 [L,QSA]

Use .htaccess to whitelist two files for execution

I have a website that has a folder for images.
I have two problems:
I want to disable all script execution in that directory (i.e. no PHP/Perl/Python
anything.)
There are two php files in my images folder called gradient.php and rgba.php – I do what those to run as per usual.
How do I set up my .htaccess file to do that. Also, rather than placing a new .htaccess in the images directory, is it possible to incorporate these directives in the one in the site root?
You can add these rules to the htaccess file in your site root:
# check if the request is for the images folder
RewriteCond %{REQUEST_URI} ^/images/
# check that it isn't a request for an image
RewriteCond %{REQUEST_URI} !\.(jpe?g|png|gif|bmp)$ [NC]
# check that it isn't a request for the 2 ok php files
RewriteCond %{REQUEST_URI} !^/images/(gradient|rgba)\.php$
# forbid access
RewriteRule ^ - [L,F]
This should make it so any request for /images/ that doesn't end with jpg/jpeg/png/gif/bmp (or whatever other extension you want to add to the regular expression) or isn't gradient.php or rgba.php, will result in a 403 forbidden.
EDIT:
I don't want them to be forbidden, I just want them to not execute – it's an upload folder, so I basically want it that if someone uploads a JPG that is secretly PHP code that I haven't detected, that it won't run
as long as jpg and other images are mapped to the correct mime/type (via AddType image/jpeg .jpg) then it won't get handed to the php handler and whatever code is there won't get executed. If you want to serve all files using the default handler, you need to set AddHandler default-handler php in the htaccess file in your images directory. You'll then need to move the gradient and rgba files out to some other directory. You can't selectively set handlers from an htaccess file, though you may be able to use <Location> blocks to set handlers in your vhost config.
EDIT 2:
I was wrong, you can use the H flag to set a custom handler using a rule. So in the above rules, instead of [L,F], you can do [L,H=default-handler] so that anything that isn't an image or gradient.php or rgba.php will get sent to the default-handler (e.g. php files will get sent as-is, and not handled and executed by mod_php).
So you can just do:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/images/(gradient|rgba)\.php$
RewriteRule ^ - [L,H=default-handler]

How manage several folders in my domain?

I want create subdomains like this:
domain.com/type/city
An examples:
domain.com/restaurants/new_york
domain.com/hotels/new_york
domain.com/restaurants/chicago
I have thousand of cities in a mysql database.
I thinked in some options:
Thousand of folders with an index.php for redirect (I think wrong way).
Create an sitemap with all links (domain.com?type=hotels&city=chicago) and manage they by code with the database.
Apache?
Please, which will be the best way for this? Thanks in advance!
You can solve this with a combination of PHP and Apache configuration. That is the most common solution and seen in popular PHP website software such as Drupal and Wordpress.
The idea is to let Apache send all traffic to one index.php file and pass the rest of the path as a parameter for PHP to handle with it.
You will need to be carefull with a few edgecases though; if file such as ./public/styles.css is requested, you don't want to serve that trough your PHP application but want apache to serve the file directly. Existing files will need to be handled by apache, all else by you application.
In your .htaccess:
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
The first line tells Apache to send normal files by itlself. Second line does the same for existing directories. Third line avoids that browsers (most notably version IE6) who request the example.com/favicon.ico don't hammer your PHP application.
Then it passes everything along to index.php and adds the rest of the path into the q param.
Inside index.php you can then read that, and take action with that:
<?php
$path = $_GET['q'];
$params = explode('/', $path);
print $path;
print_r($params);
?>
Thousands of folders would be the wrong way that is for sure.
If you start creating the sitemap with links of the type domain.com/?type=hotels&city=chicago you get a nice structure that you can manage programatically.
First get this started and working, then look up .htaccess and mod_rewrite which you can then use to map from domain.com/type/city to your links already functioning.
This seems both to be a good strategy for getting something working fast, and for ending up with the prettiest solution.

Mapping folder to a different server via htaccess

Our website currently servers images from a website.com/images/ folder.
Thing is we have limited bandwidth on that server and the images are sucking it up.
We have another server with a lot more free bandwidth.
Is there any way to maps /images/ to goto the other server where we'll upload that folder? This would save us from needing to change the paths of every image across all the pages.
RewriteEngine On
RewriteRule ^images/(.*) http://your.images.storage.site/images/$1 [L,QSA,R=302]
It requires Apache to have mod_rewrite installed.
try this:
RewriteEngine on
RewriteRule ^images/(.*)$ http://otherwebsite.com/images/$1 [L,R=302]