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