I am hoping to have some .htaccess expires headers stuff be conditional based upon the current environment. For example, the following would only be active if it were the production server, as opposed to local development:
ExpiresByType image/png "access plus 1 month"
It is annoying to have to clear cache during development constantly. Any ways around this?
Thanks :)
Yup, use your browser in Private AKA "Porn Mode" during testing. Chrome allows this on a per window basis. In this mode, any caching, 301 redirect info is cleared when you close and reopen a session. You can also clear history over the last hr / day / week. This is what I do.
In terms of Expires, etc. AFAIK there is no mechanism for embedding conditionals for ExpiresByType however is its trivial to bookend such lines with "##--START-DEV" "##--END-DEV" lines and write a trivial sed script (or windows equivalent) to rewrite the .htaccess commenting out or re enabling lines between such bookends.
Related
I would love it if my Perl script can change the address in the browser. Not to try and hide things, or be funny. But for cleanliness sake.
www.example.com/billing
looks much better than:
www.example.com/cgi-bin/billing.pl?userid=0000&inv=1234
I realize browsers probably have high security measures to prevent phishers from being more tricky, but what gives me some hope that this is possible is that I placed the following line in my .htaccess file:
ErrorDocument 404 /index.html
and if I go to www.example.com/nonexistent the browser will go back to the main page, but the address bar still has the nonexistent address.
Thanks
I've got a site that uses a lot of SVGs as background images. Many of the SVG files have query strings attached to them, which are used to dynamically change colors (via a bit of PHP). For example, my CSS might call an SVG like this:
background-image: url("/images/sprite.svg?color=00ffff");
I've now realised that these SVGs with query stings don't get cached which makes page loads very slow and clunky!
I tried adding this to my .htaccess:
<FilesMatch "\.(svg)">
ExpiresActive on
ExpiresDefault "access plus 1 month"
</FilesMatch>
But that doesn't seem to do it – looking at the Network tab of Chrome Dev Tools shows that they still have a size, rather than being "from cache".
Is it actually possible to tell a browser to cache SVGs with query strings? Is my htaccess rule wrong? Or is there another reason that Chrome wouldn't be caching them?
You should use an ExpiresByType directive instead. See mod_expire:
ExpiresActive on
ExpiresByType image/svg "access plus 1 month"
I've a pretty large htaccess about 400 lines and 30 of theme are filled with "RewriteEngine on" do I really have to place it every time before a script or is one time in the first line of the htaccess enough?
Thanks, Sake
As long as it's in the same scope as the rest of your rules, you only need it once, and it doesn't even need to be at the very top.
By scoping, I mean:
<Files "*.php">
RewriteEngine On
(some rules)
</Files>
(some more rules)
isn't going to work, because if the request isn't for a PHP file, the RewriteEngine On isn't going to get applied, and the "some more rules" part won't do anything.
So I usually want to set htaccess rules slightly differently based on what server it is on, eg live or development.
The ErrorDocument usually needs to be different, as well as some of the AddType and SetHandlers bits.
Is there any way I can make this a bit more dynamic and get it to auto detect based on the URL, then set a variable and have if conditionals further down in the htaccess?
Want to do this entirely from URL detection instead of setting parameters with apache please :)
No there isn't any way to set those things via some url detection. You can't make normal if conditions surrounding some of the things you want (AddType SetHandlers and ErrorDocument).
You could use env variables and mod rewrite but I don't think you'll like the end result. You'll have to do something like this using env|E=[!]VAR[:VAL] syntax
If you were in the httpd.conf or vhost file you might be able to separate your different setups by using <directory> sections </directory>. But Directory is not allowed in htaccess.
Also I wouldn't do this in a production environment anyways since something could go wrong and I would think the detection is slower and not needed. Perhaps you may want to look into a build script you run to create/deploy your different setups for development/production depending on hostname and other factors.
One of my friend says, etags on off mode for speed up page loading. How to off etags with htaccess?
To unset Etags use 3 lines below:
Header unset Pragma
FileETag None
Header unset ETag
But, to increase page loading speed, my advice is YSlow.
perfect tool & Yslow recommends to use Etags instead of setting off.
Putting the following in your .htaccess should do the trick.
Header unset ETag
FileETag None
Note: Disabling etags only helps if you are hosting the same content from more than 1 server (such as when using a cluster or CDN). Take a look at wikipedia or yahoo best practices for more information.