×

how to Serve Image media files from Cookie Less Domain

Must Have Plugins

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.

rakesh

The Whole concept was developed by the man you are watching here. I am rakesh Kumar Serial Niche Blogger and SEO Enthusiast. find me on Google+ , facebookandon twitter.. Developed popular WordPress theme for Wallpapers and funny Image WordPress theme. Love SEO optimized WordPress theme Designing and Customization.
Related Posts

22 thoughts on “how to Serve Image media files from Cookie Less Domain”

  1. 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

  2. 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

    1. 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

  3. 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.

    1. 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

  4. 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!

  5. Hi Rakesh,

    One question: I took all the steps required but still things are not as supposed to be.

    All my old images are on the subdomain, but the new images I upload for the articles go to mydomain.com/imagefolder/year/month/image.jpg instead of imagefolder.mydomain.com/year/month/image.jpg

    I’m mostly curious how did you “assigned” a subdomain to the wp-content/uploads directory.

    Many thanks!

  6. Is it just me, or the none of the images in the article show up? Because I can’t see any of them, just the “alt” tag.
    And I think they were kinda crucial in understanding the whole process.

  7. Wow!Its unique and good one buddy.I came to know first time about,will try hands on.Hope I able to get maximum out of it.Thanks for all..

  8. Nice info which will be prove so useful for all blogger and online marketers. That kind of activities also make very good and nice way to search our destination.

  9. We have a simple solution – wordpress can define for which domain it should store the cookies.

  10. Kudos for the post Ragesh,I never tried it before but I think it’s time to test it out :) Before doing any changes we need to consult with our theme provider ?

    1. I think nothing special will happen if you do this with any theme as we are not changing the default path of image folder. Just defining that folder with a sub-folder.

  11. What if I want to move all javascripts and CSS files to that sub-domain?? Is it possible? Will this affect load time?

    1. This will surely improve your loading time. but instead of that i would suggest you to check your theme when your javascript and CSS files are loading. Since comments are only required on a single post, you can load such CSS only on that time and the same way when javascript is required load only at that time. This way without using any CDN or cookie less domain , still you will be able to load your site faster.

      See you very soon. ;)

Leave a Reply

Your email address will not be published.

+-