Is there any way to save the css files of my site that are loaded with the tag at users' browsers for future use. I mean that I do not want the users to reload the css files each time they visit my site cause it makes the site slower. Can I achive this with html, javascript or php?
Thank you for your time!
It's good practice to leverage browser caching, as can be read here.
How you can do this is by editing your .htaccess file like so (example):
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType text/javascript "access 1 month"
ExpiresByType image/x-icon "access 1 year"
</IfModule>
As you can see, with this code css files will be cached for 1 month.
Be careful setting your times, because the browser will not reload the file unless the time has expired or the name of the file has changed. An easy way to circumvent this would be to use versions in your file like so: main.css?v=1.0. The upside of this is you don't have to rename your files everytime, but downside is most proxies don't cache resources with a "?" in the url.
Related
I run on a Windows server and want to cache different resources for different time periods: some stuff never changes, and other stuff changes every few weeks.
If I had Apache, I could use .htaccess to set up a list of commands like this (actually, this is what I used last year):
ExpiresByType image/jpg "access plus 100 days"
ExpiresByType image/gif "access plus 100 days"
ExpiresByType image/jpeg "access plus 100 days"
ExpiresByType image/png "access plus 100 days"
ExpiresByType text/javascript "access plus 100 days"
But I don't. IIS has nothing that I can see that's so specific, so perhaps I need to do this with PHP.
I've read a whole host of articles, but none give specific advice on how to achieve the same result as the .htaccess solution above.
Ideally, make it page-specific, which is where PHP might come in ... maybe some statements at the top of each page?
Any ideas?
i have a single .htaccess file for both admin panel and website.
In my website I am using apc cache for cache and it works fine in website,
but in backend/admin panel there are lots of issues due to cache.
now i don't need a cache in admin panal but website i need a cache
So, please tell me how I can write a regex in .htaccess for both website as well as admin panel.
Currently I am using this code in my .htaccess
## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/svg+xml "access 1 month"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 2 days"
</IfModule>
## EXPIRES CACHING ##
For example :
cache works on : www.xyz.com/
cache not works on : www.xyz.com/admin
It looks like you are talking about assets like images, stylesheets and javascript.
Assuming that the assets are the same for the normal sections and the admin section, there is no easy way to do that in your .htaccess file: The browser will request the same urls, regardless whether the user is visiting a normal page or an admin page.
What you could do in your admin section in the code itself, is invalidating the cache by using unique filenames like /assets/css/style.css?v=UNIQUE_VERSION_NUMBER.
Here the unique version number could be the hash of your latest commit, a time-stamp, etc.
You can turn off by caching to a specific folder by using following code.
<Directory "your folder path(e.g. /www/htdocs/admin)">
ExpiresActive Off
</Directory>
Hope this may resolve your issue.
I've set up a htaccess file to cache data on my website for speed optimization.
However, I have the issue that some of my "external" sites on my webserver are therefore not updated unless forced to do so. Specifically, the following page I would like to avoid having to change version name on to update:
http://canlogger.csselectronics.com/downloads.php?q=canvas
I.e. this page loads content from a folder on my server unto a website. The problem is that the files in the folder, as well as the loaded release doc info is not updated automatically as it has been cached. This I would like to avoid (but keep caching for e.g. images etc.)
My htaccess looks like this:
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 week"
ExpiresByType image/jpeg "access 1 week"
ExpiresByType image/gif "access 1 week"
ExpiresByType image/png "access 1 week"
ExpiresByType text/css "access 1 week"
ExpiresByType text/html "access 1 day"
ExpiresByType application/pdf "access 1 week"
ExpiresByType text/x-javascript "access 1 week"
ExpiresByType text/javascript "access 1 week"
ExpiresByType application/x-shockwave-flash "access 1 week"
ExpiresByType image/x-icon "access 1 week"
ExpiresDefault "access 1 day"
</IfModule>
Any help would be appreciated!
Martin
First off, you shouldn't be using .htaccess files for this. On every single request, your web server has to load it, really making things inefficient. You should use a proper web server configuration that's loaded on start.
Next, you shouldn't let the web server dictate the caching control in this case. Your PHP script knows when its underlying data changes and can handle creating proper cache control headers, as well as generating an appropriate etag. See also: https://stackoverflow.com/a/4485194/362536
I'm new here so sorry if this is in the wrong category or something like that. Now getting to my problem. I have a website: www.windowstaan.pe.hu which also opens on a mobile app so in order to make it load fast I have enabled caching in .htaccess and here's its code:
## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 day"
ExpiresByType text/html "access 1 day"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>
## EXPIRES CACHING ##
Now what happens here is that any page I load in the browser can also be loaded offline which is what I wanted, but if I make any changes to the html then I'm not sure how its going to update here.
My html files are saved as index.php, PHP because they have some PHP code in them mainly include header, footer and a small mysql line.
Now what I want is for this homepage and every other html/php page to reload every day.
Please tell me what I need to do in .htaccess to set it like that.
I have tried setting html / css to 1 day there but that is not working perfectly as my app still loads the old cache. (My app is just a browser of windowsphone 8.1, IE11 probably.)
Thanks in advance.
The browser caching will base on the resource URL.
When you want to force browser to reload the new resource, you just change the URL by adding a version string.
For example, current image URL:
http://static.adzerk.net/Advertisers/af217662e49a4cbda030feae88418cdd.png
You can change to:
http://static.adzerk.net/Advertisers/af217662e49a4cbda030feae88418cdd.png?v=1
The version number can be controlled in your PHP code.
Version control number can be read/write from a text file in case you do not use DB.
I have a website with dynamic pages.
I want to set the cache for images (all my gif, jpg and png) and css files to a specified time (1 month)
I want the browser to confirm if the files are still same on the server before serving cache copy.
How can i go about it via PHP?
We do this at the web server level, apache in our case (this question is also tagged with apache) using mod_expires.
You could do this with the following directives in your httpd.conf, or similar:
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType text/css "access plus 1 month"