5

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
max
  • 3,614
  • 9
  • 59
  • 107
  • Can you enable `RewriteLog` and post the logs here. – anubhava Oct 21 '13 at 21:04
  • Also `RewriteRule ^(images/.*)$ http://site.com/files/images/$1[R=301,NC,L]` needs some correction as `RewriteRule ^(images/.+)$ http://site.com/files/$1 [R=301,NC,L]`. Do make sure you're using a new browser to test it. – anubhava Oct 21 '13 at 21:06
  • @anubhava i've tried to enable `RewriteLog ` in `httpd.conf` by changing `Loglevel Warn` to `LogLevel alert rewrite:trace3` as it was suggested here http://httpd.apache.org/docs/current/mod/mod_rewrite.html .... but when i restart apache i get an error `/sbin/service httpd restart 2>&1` apparently it doesn't work or i'm doing something wrong – max Oct 21 '13 at 23:13
  • See this link for enabling rewrite log: http://stackoverflow.com/questions/9632852/how-to-debug-apache-mod-rewrite Also did you try suggested rule above in my comment. – anubhava Oct 22 '13 at 06:52

4 Answers4

1

You should redirect to the right place. In the htaccess file in your public_html/img directory do:

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

Or just

Redirect 301 /images http://site.com/files/images
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • is there any way to include subdomain in the rule ? there are lots of other `images` directory in my subdirectories (in `assets` directory for example ) and using this rule would mess them up . i've tried to change `^images/(.*)$` to `^site.com/images/(.*)$` but its didn't work – max Oct 17 '13 at 16:27
  • if you have dedicated vhost for domain img.site.com, you can add above Redirect to vhost, not to .htacces, then it will be apllied only for requests to img.site.com/images not for all */images – ziollek Oct 22 '13 at 19:25
  • @max You can include the subdomain by using the `%{HTTP_HOST}` variable in a conditional match (see edit) – Jon Lin Oct 22 '13 at 19:30
1

Ok try this:



RewriteEngine On

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


1

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.

Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
  • Yes, I'm going through it as we speak. – Ravi K Thapliyal Oct 20 '13 at 23:35
  • also check this out , maybe it has something to do with this ... http://stackoverflow.com/questions/19071324/request-exceeded-the-limit-of-10-internal-redirects .... thanx again – max Oct 20 '13 at 23:37
  • (1) Is your htaccess inside `/public_html`? (2) Make sure there's a space between `$ [R=301,NC,L]` (3) Is the file you're trying to access as `http://img.site.com/images/2013/10/image.jpg` accessible when you go to `http://site.com/files/images/2013/10/image.jpg`? – Ravi K Thapliyal Oct 21 '13 at 00:03
  • 1 - yes , 2 - it is , 3- yes – max Oct 21 '13 at 23:14
  • 1
    I think whatever config your cpanel does when you create a subdomain is interfering with the htaccess rules. If you have wildcard dns enabled (i.e. `*.site.com` points to `site.com`) you would be able to reach your site's main page from `anything.site.com` as well. Can you try setting up the `htaccess` rules for `images.site.com` instead and see if the files become accessible? Basically, I want to pull any server config that's forcing `img.site.com` to go through `/img` out of the equation. – Ravi K Thapliyal Oct 22 '13 at 11:11
1

well I can provide an indirect solution where you have to make a little changes in your header or footer files, and for that, type below lines in your .htaccess,

HeaderName /path/.../header.html (if .html) or if .php header then,

AddType text/html .php
Addhandler application/x-httpd-php .php
HeaderName /path/.../header.php

or simply ReadmeName /path/.../footer.html for footer, then use copy+unlink (for moving files but slow one), (or precisely rename() would be a better option), in that file for your task.

Shivanshu
  • 1,230
  • 1
  • 11
  • 17