Mensur
Mensur

Reputation: 475

read images from root directory in subdomain with htaccess

I have a domain like example.com where root directory is web. I have created a subdomain happy.example.com where directory is outside web folder called happy and connected it to happy.example.com.

My webpage tree looks like

happy
web/images

So all my images for root directory are stored in (web/images)

example.com/images

So a full path to an image can be

example.com/images/me.png 

Now i have created a sudbdomain which i call:

happy.example.com 

What i would like to do is if i type

happy.example.com/images/me.png

then i should be able to see the picture. So somehow i need to link all images folder to a subdomain from root directory in web. I hope you guys got my question. I guess i shoud have an htaccess file with all funny stuff in happy folder?

Cheerz

Upvotes: 0

Views: 1357

Answers (1)

Jon Lin
Jon Lin

Reputation: 143856

Since the two document roots of your two domains aren't connect to each other (one inside the other, or the same), you'll need to either redirect or proxy, or, use a script.

To redirect is easiest, but it'll change the URL in the browser's location bar:

Redirect 301 /images http://example.com/images

or using mod_rewrite:

RewriteEngine On
RewriteRule ^images/(.*)$ http://example.com/images/$1 [L,R=301]

To proxy, you need to have mod_proxy loaded, which isn't always the case if you're using a webhost or hosting service. But you can use the P flag in mod_rewrite to do this:

RewriteEngine On
RewriteRule ^images/(.*)$ http://example.com/images/$1 [L,P]

The last option is to route all image request to a script, like a php script or something. And the script itself returns the image:

RewriteEngine On
RewriteRule ^images/(.*)$ /load_image.php?image=$1 [L]

then you'd have something like:

<?php
    header("Content-type: image/jpeg");
    readfile('../web/images/' . $_GET['image']);
?>

Obviously, you'd need to check the extension and return the correct content type depending on the image type.

Upvotes: 2

Related Questions