Redirecting images in WordPress

This week I have had the opportunity for go to a WPO event that reviewed how rise the performance of a blog based in WordPress and I decided apply some ideas that were presented there in my blog.

One of the things that were spoken is that necessary separate the statics (or uploads) of the principal domain for lower the time of loading page (because much of popular navigators loads the contents in parallel mode if they have different domains between they).

In my personal case, I wanted to keep the same directory that WordPress have by default, because the structure that have WordPess by default is correct for my personal blog use.

So the unique change that you need is modify the virtualhost configuration adding other virtualhost that link to the default directory when WordPress save the statics.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<VirtualHost *:80>
    RewriteEngine On
    RewriteOptions Inherit
    DocumentRoot /web/httpd/wp-content/uploads
    ServerName w1.statics.net
    FileETag All
    <Directory "/web/httpd/wp-content/uploads">
        Allow from all
        Options +Indexes FollowSymLinks
        AllowOverride All
    </Directory>
    <IfModule mod_expires.c>
        ExpiresActive on
        ExpiresDefault "access plus 1 month"
    </IfModule>
    <IfModule mod_headers.c>
        RequestHeader unset Cookie
        Header unset Set-Cookie
    </IfModule>
    <IfModule mod_deflate.c>
        DeflateCompressionLevel 9
        <FilesMatch "\.(js|css|php)$">
            SetOutputFilter DEFLATE
            BrowserMatch ^Mozilla/4 gzip-only-text/html
            BrowserMatch ^Mozilla/4\.0[678] no-gzip
            BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
        </FilesMatch>
    </IfModule>
</VirtualHost>

But Now, is when Google arrives and tell us that you have two different urls with the same content.

  • http://web.com/wp-content/uploads/year/month/image
  • http://w1.statics.net/year/month/image

If you blog is new and Google have not already yet indexed, is not necessary make changes but we would risk someone (with bad intentions) began to link images with the url of the website and hassling life.

In my case, I don’t take here because I had images in old post with old urls and I needed redirect this urls somehow.

After more attempts, I discovered how make it. You need add new redirection directive in the virtual host blog, just after when we define the directory. And I say that make in the virtual host because I tried to make in the .htaccess file (that I should be better) but I could not make it work (if someone has done it, please tell me how it has and I greatly appreciate).

1
2
3
4
5
6
7
8
9
<VirtualHost *:80>
    ServerName web.com
    ...
    <Directory "/web/httpd">
        ...
    </Directory>
    RedirectMatch 301 ^/wp-content/uploads/(.*)$ http://w1.statics.net/$1
    ...
</VirtualHost>

And that’s all. With this changes we are finished the problem with the different url with the same images. Now you only need configure in the WordPress the new domain for the statics and change all the images that you have in the old posts and pages one by one. This is simple if you don’t have many images but, in the opposite case, this is a Chinese work.

I’m working that a plugin for make this automatically but if you know any plugin that make it, please tell me, because I don’t found any.

Leave a Reply

Your email address will not be published. Required fields are marked *