how to Serve Image media files from Cookie Less Domain


Kindly Note: It is always advised before trying such critical updates on your live website. Always perform your complete database / website backup. We will not be any how responsible if any mis- happening occur while following this how to

Google Page Speed and Yahoo YSlow both clearly suggest that contents that does not generate Cookies must be served from Cookie Less Domain or sub-domain

Another Benefit from serving cookies less contents from a separate domain or from sub-domain is – New age browser like Firefox , chrome make only limited connection at a time from a domain this serve your contents slowly. This method increase loading speed.

By default media files such as images, video, audio files stored on wp-content/uploads folder, and initially we also does not care unless until we have a solid wordpress/ web-development experience.

Here we have two solutions for the same problem

1. Buy a separate Domain and Web-space, Store all our media files in its public folder and suggest this path in our wordpress media folder.

2.  Create a sub domain. Store all our media files in this sub-domain and again suggest  our media folder to look into this sub-domain.

Problems in these Approach

1.  Problem in first solution is. Small bloggers does not have sufficient funds to invest in a separate domain and web-space to host their media files.

2.   If you are shifting your current folder from wp-content to this new domain / Sub-domain. Then the absolute links stored on your database remain same and does not update itself automatically. Thus  can broke down your whole website.

3.  Sub-domain also shares the cookies from the main domain. So the second method directly does not work.

Solution – Set a Cookie Less Sub Domain

Kindly read this section very carefully, otherwise i am doubtful , You might mesh up  the whole process.

1.  Create a sub-domain. Login in to your Cpanel administrator screen and from here create a new sub-domain

2. Give it a clear new identity so that you can instantly recognize it. like media.binarynote.com . Here i would love to store all my media files currently. placed in wp-content/uploads folder

Note : I have not created a new sub-folder inside public_html folder instead of that used the default wordpress media folder and assign this a sub-domain.  Latest version of wordpress does not allow to read folder above it. Thus leave your all efforts useless and can also break your whole website.

Most Crucial Part of this While Process now begins

2. Change the Location of your media files from wp-content/upload to this newly defined domain/Sub-domain Name from you wordpress Admin Panel->Setting-Media.

media settings for cookie less domain

3.  By default wordpress store absolute path of the relative images and this create a lots of problem whenever we change our server. SO how to change the image paths stored in the post contents. use the following sql command to change the absolute path of it’s image files

update wp_posts SET post_content = REPLACE (post_content, 'http://binaryNote.com/wp-content/uploads/','http://media.binarynote.com/');

in your case replace your domain name and sub-domain name with the actual domain name of yours

Must read :  15 SQL queries for wordpress web-Administrator

But by default what ever cookies created on main domain will be shared on all its sub domain also.

So the problem remains same – Yes? . No , we have a simple solution – wordpress can define  for which domain it should store the cookies. So these cookies will not be shared on all its sub-domains

How to Solve Cookies Related Problem

4. Now it’s Time to solve this Cookies related problem – Open wp-config.php file in your favorite editor. My favorite is Notepad++ and add the following code.

define(“COOKIE_DOMAIN”, ‘www.binarynote.com’);

The above statement directs wordpress to generate Cookies for it’s main domain not for its sub-domain.

Tools.pingdom.com and Yslow are their to recheck total loading time of your website and its overall score.

Conclusion

Hope You have followed all the steps properly and now able to serve your images from a Cookies less Domain. If you still have some queries regarding this hack. Kindly do not hesitate to send your queries/suggestion.




22 Brilliant Comments - Join Discussion Now!

  1. Stow says:

    Rakesh, thanks for your time and advices.
    I’ve finally managed to accomplish what I wanted with this tutorial:

    http://wordpress.org/support/topic/uploads-folder-outside-wp-root?replies=13

    the “/home/user/public_html/images” does the trick!
    Although I’ve just found that setting “define” cookie domain in wp-config is making the admin and any user unable to login. So I’m kinda back to where I’ve started but at least the first part, serving the path correctly is happening.

    Thanks again!

  2. Dijin says:

    Thanks to sharing the good ideas like this..

  3. Thanks Rakesh, I am also thinking of doing this steps to reduce the loading time of my site.

  4. Arup Ghosh says:

    If someone hosts the subdomain in the same hosting , no improvement in the loading speed will occur as the same server has to face the load of two different domain.

    • rakesh says:

      But i have different idea for the same, this is the reason i have written this article and believe me there was significant improvement in my site speed. BTW thanks for this update arup. Keep in touch

  5. Somesh says:

    Still the speed is slow. I don;t know why but it is hard to find out.

  6. chris says:

    Hey, do you know how to do this on Joomla? There is cookie settings in joomla global configuration? I tried to move images on new subdomain and link them over css but yslow again shows that new subdomain is not cookie free. I guess something needs to be added to .htaccess or somewhere else? Thanks

    • rakesh says:

      Dear Chris, I do not know how to do this in Joomla, but i am sure if you would ask the same question in joomla forum or search any such extension in their repository, then i am sure you would get any feasible solution over there. Once i was also a joomla guy but due to these types of problem, switched on WordPress-a very easy to use and update blogging system. Keep in touch

  7. Somesh says:

    I’ve tried everything you mentioned here but, when i try to upload new images it shows a broken image icon in place of that and nothing happens.
    Tell me how to fix this issue

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>