Forum Moderators: phranque

Message Too Old, No Replies

Set up Expire Headers - Can I load modules from .htaccess?

(because i don't have root access)

         

diddly

1:54 pm on Feb 15, 2015 (gmt 0)

10+ Year Member



Hello,
I've been having problems on this server (
Apache/2.2.22 Debian
) before, and I cannot tell if my noobishness is to blame or if it is misconfigured.
In any case I do not have root access. I can look at config files outside my home, but not change them.

Right now I'm worried about Expire Headers.
Yslow is complaining that
There are 5 static components without a far-future expiration date [and] misconfigured ETags.
(this list does not include the php-generated html document itself, but all images and css files)

So I tried adding some via .htaccess, but it seems the modules in question are not loaded:
  • mod_headers
  • mod_expires

both throw a 500 error when I try to use them (meaning config options targeted at these modules), and have no effect when I enclose them in
<IfModule ...>
clauses.

I can look at the loaded modules with
phpinfo();
:
core mod_log_config mod_logio mod_version prefork http_core mod_so mod_alias mod_auth_basic mod_authn_file mod_authz_default mod_authz_groupfile mod_authz_host mod_authz_user mod_autoindex mod_cgi mod_dav mod_dav_svn mod_authz_svn mod_deflate mod_dir mod_env mod_mime mod_negotiation mod_passenger mod_perl mod_php5 mod_reqtimeout mod_rewrite mod_setenvif mod_ssl mod_status

I can see from searching the server that more modules are available, also mod_headers and mod_expires.

Is there a way to load them from .htaccess?

I tried with mod_so [httpd.apache.org] but i'm not even sure it is the right thing and after a couple more 500 errors i realized that i need more information.

Is there a way to configure expires headers without apache/.htaccess?

lucy24

8:51 pm on Feb 15, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Is there a way to configure expires headers without apache/.htaccess?

Well, you could put an "expires" meta in each and every page's HTML. But let's first see if we can figure out why Expires directives aren't working in htaccess. I assume your htaccess works in other situations, so it's not a problem with overrides? It isn't 100% clear from your post whether it's actually your own server or shared.

What exact commands did you try? Did you remember ExpiresActive?

:: detour to Apache docs ::

The override category for mod_headers is "FileInfo", same as for mod_rewrite and so on. For mod_expires it's "Indexes". I can't imagine that being disabled, unless overrides aren't allowed at all.

:: further detour to test site to check something ::

I think-- but don't quote me-- the <IfModule> envelope doesn't strictly mean "does this module exist". It means "can I use it here".

diddly

5:08 am on Feb 16, 2015 (gmt 0)

10+ Year Member



Hello,
this is a friend's server and i can use it as a normal user, no root privileges.
there's no admin panel that i know of, just straight linux (sshfs and ssh to connect and edit files).

although that friend definitely has some experience with web development, i can't shake the feeling that some things are seriously misconfigured there.

but i got the server space for free, and he's still making business with it, so i don't really feel confident enough to demand changes...

anyhow, this is my webroot directory's .htaccess:


AddDefaultCharset utf-8
<IfModule deflate_module>
# Enable compression for the following file types.
AddOutputFilterByType \
DEFLATE \
application/javascript \
text/css \
text/html \
text/javascript \
text/plain \
text/xml\
text/php
</IfModule>

# allow access from all domains for webfonts
<FilesMatch ".(ttf|otf|eot|woff|font.css)$">
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>
</FilesMatch>

# Cache Files 1 Day
<FilesMatch "\.(jpg|jpeg|png|gif|swf|js)$">
<IfModule mod_headers.c>
Header set Cache-Control "max-age=86400, public"
</IfModule>
</FilesMatch>

ErrorDocument 404 /error.php
ErrorDocument 403 /error.php
ErrorDocument 500 /error.php

AddType application/vnd.ms-fontobject .eot
AddType font/ttf .ttf
AddType font/otf .otf
AddType application/x-font-woff .woff

#LoadModule expires_module modules/mod_expires.so
#AddModule mod_expires.so

<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 1 days"
# My favicon
ExpiresByType image/x-icon "access plus 1 year"
# Images
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
# CSS
ExpiresByType text/css "access plus 1 month"
# Javascript
ExpiresByType application/javascript "access plus 1 year"
</IfModule>



the deflate module seems to be working, all others throw a 500 error as soon as i remove the <IfModule...> clauses around them.

i can't even think of a way to set expires for other documents besides the main html document, other than through .htaccess.
maybe php offers something...
i'm not even sure if Yslow's results are to be trusted. trying it on webmasterworld.com, the results are better but basically it bemoans the same things: expires headers for external files and ETags.

i agree that solving this through .htaccess would be best.
thanks for taking an interest.

not2easy

6:56 am on Feb 16, 2015 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



My notes say that the
mod_expires
module is what controls Expires and Cache-Control HTTP headers, but you have it two ways, each with a different setting.

<IfModule mod_headers.c>
Header set Cache-Control "max-age=86400, public"
</IfModule>


allows image cache for 24 hours, then further along
mod_expires
allows one month for images. I need to check but pretty sure that
mod_headers
lets you customize headers but not set Cache-Control. (?) I have not seen DEFLATE done quite that way before, but it isn't something I look at every day, it may well be an alternative I'm not familiar with. Usually seen them as:
AddOutputFilterByType DEFLATE text/plain text/html
AddOutputFilterByType DEFLATE text/css text/javascript application/javascript application/x-javascript

for each line of file types.

None of the caching directives above is for html files, so that would use your 1 day default. Files that are not on your server can't be assigned any caching properties from your server. It is good to deal with the files you control and not be too concerned about files/resources on other servers.

All the page speed analyzers I've seen show those external file caching issues, but those are not things you have control over. Google's Page Speed tester drove me nuts over a <div class="abgd"> on my sites that wasn't in any of my pages or css - I found it in their own AdSense Ads which have long been a source of picky errors.

lucy24

7:40 am on Feb 16, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



AddOutputFilterByType \
DEFLATE \
application/javascript \

wtf? Where'd that syntax come from? Apache docs (and my own htaccess) say
AddOutputFilterByType filter[;filter...] MIME-type [MIME-type] ...

meaning that you first list the different kinds of filtering you want, such as DEFLATE, and then in the same line list the filetypes you want the filtering to apply to. You can put each filetype on a separate line, but each line has to be a complete statement.

all others throw a 500 error as soon as i remove the <IfModule...> clauses

:: detour to test site to verify that this is the form the server expects to see ::

That does sound as if mod_deflate isn't available. But it seems such a ridiculous thing to exclude.

:: further study of Apache docs ::

Because of certain problems discussed below, this directive is deprecated. The same functionality is available using mod_filter.

Well, now they tell us. But the writeup on mod_filter gives me a headache, so let's ignore that part. Besides, 2.4 uses the identical "AddOutputFilterByType" syntax within mod_filter, so I don't know what they're on about. (In 2.2 this directive is considered Core, even though it requires other mods to work. Go figure.)

although that friend definitely has some experience with web development, i can't shake the feeling that some things are seriously misconfigured there.

You will need to lean on your friend and find out what the Overrides settings are for the directory that contains your site. And for heaven's sake find out which modules you've got, so you can ditch all those <IfModule> envelopes.

diddly

9:52 pm on Feb 16, 2015 (gmt 0)

10+ Year Member



im sorry about my messy .htaccess; i removed the 2 offending bits mentioned above. more about that further down.

to summarize:

the phpinfo() function tells me which apache modules are loaded.
it produces a long table; the desired info is under configuration => apache2handler => loaded modules.
i already pasted it in post #1 - neither mod_headers nor mod_expires are in that list, but i can find the files mod_headers.so and mod_expires.so under /usr/lib/apache2/modules.

why does every .htaccess tutorial i find ask for mod_expires.c, and not for mod_expires.so?

but even so, the logic is overwhelmingly clear: whenever i add a line to my .htaccess that is to be interpreted by one of those modules, the server throws an error, but if i enclose that line in an IfModule clause, there's no error.
e.g.:
ExpiresActive On 

=> error

<IfModule mod_expires.c>
ExpiresActive On
</IfModule>
=> no error

no error, but no caching either, according to Yslow and gtmetrics.

i sent a message to the server's admin, but meanwhile i'm back to my question:

can i load a module via .htaccess? (actually i think i know the answer: not without re-initializing the server)

can i explicitely set expires not only for the document itself, but also for secondary content (images, css, all of which on the same server)?

there's the <header> section in the html document, and there's the http header itself (i found a php function that can add values to that) - are they the same thing?

ok, i cleaned my .htaccess [codepad.org] up a little and also added some stuff. everything is fine like this. i uncommented all the if-clauses, although i now they return false. just for now, until this is solved.

not2easy

10:34 pm on Feb 16, 2015 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



no error, but no caching either, according to Yslow and gtmetrics.

That's because you removed all the settings you had in place. Turning it on doesn't do anything, after it is on you need to give it instructions for what you want it to do. You had Instructions, you might want to put them back and add a line for html or set the default for more than one day.

phranque

12:26 am on Feb 17, 2015 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



http://httpd.apache.org/docs/1.3/mod/core.html#addmodule
AddModule Directive
This directive has been removed in current versions of the server. See LoadModule


http://httpd.apache.org/docs/current/mod/mod_so.html#loadmodule
LoadModule Directive
Context: server config, virtual host

this context specification means you can only activate modules in the httpd.conf file, not in .htaccess.

lucy24

5:27 am on Feb 17, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



but if i enclose that line in an IfModule clause, there's no error.

That's because the module is not recognized-- whether because its name is misspelled or because it isn't available to the present htaccess file. So the whole <IfModule> envelope is ignored as if the lines simply didn't exist. That's what the envelope is for.

I too was scratching my head over the .c vs .so naming thing. Apache docs say you can give the name in either of two forms, loosely "mod_name-of-mod.xtn" and "name-of-mod_module".

can i load a module via .htaccess?

No. It's server or nothing. The only thing you can do in htaccess is enable/disable the functionality of a module that's already present.

diddly

6:38 am on Feb 25, 2015 (gmt 0)

10+ Year Member



well, thanks.
i contacted server admin a few days ago, no reply so far.
i guess i'm back to the question from my other thread:
[webmasterworld.com...]