Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to change a subdomain's directory with .htaccess?

I have an image sharing website and I used to have a sub-domain for the images, however I had to delete it, so now I use a real path to the images:

old url:

http://img.site.com/images/2013/03/abc.jpg

new url:

http://site.com/files/images/2013/03/abc.jpg

The problem with this is that I have a lot of images linked from other websites and search engines that are still using the old URL.

The problem with the old sub-domain is that it's directory (public_html/files) and its name (img.) are different, so special configuration was needed to handle that and I got tired of all the problems that it was causing.

Now I have created a normal img. sub-domain that is pointing to the public_html/img directory so that no extra configuration needed, however my images are still in the public_html/files directory.

I figure all I have to do now is replace the new directory with the old one using .htacess somehow. I don't know if this is possible.

My .htaccess is currently:

<IfModule mod_rewrite.c>

    RewriteEngine On

    RewriteBase /
    RewriteCond %{HTTP_HOST} ^img\.site\.com$ [NC]
    RewriteRule ^(images/.*)$ http://site.com/files/images/$1[R=301,NC,L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{ENV:REDIRECT_STATUS} ^$

    RewriteRule ^(.*)$ index.php/$1 [L,QSA] 
</IfModule>

When I try to call an image from a subdomain I get:

Not Found

The requested URL /files/images/2013/10/15c100d1da3216850d6cceb4ce57eaab6d26fc92.jpg was not found on this server.
img.site.com

My error log reports:

[Mon Oct 21 02:58:46 2013] [error] [client 85.185.229.221] script '/home/site/domains/site.com/public_html/img/index.php' not found or unable to stat
like image 782
max Avatar asked Oct 02 '22 16:10

max


2 Answers

Ok try this:



RewriteEngine On

RewriteCond %{HTTP_HOST} ^img\.your-domain\.com$ [NC] 
RewriteRule ^images/(.*)$ /files/images/$1 [L,R=301]


like image 139
entrapeneur Avatar answered Oct 13 '22 01:10

entrapeneur


Your modified .htaccess would look like (assuming its placed at document root /)

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{HTTP_HOST} ^img\.site\.com$ [NC]
    RewriteRule ^(images/.*)$ http://site.com/files/$1 [R=301,NC,L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

The RewriteCond first checks the domain and if it's the old one img.site.com, redirects any /images requests to the new URL.

like image 27
Ravi K Thapliyal Avatar answered Oct 13 '22 00:10

Ravi K Thapliyal