1

I have want cookie-free subdomain, to improve yslow,

Can i in htaccess check witch hostname is use

In my case if www-static.example.com is called, then i want to set

AddDefaultCharset UTF-8
ServerSignature Off
Options -Indexes
FileETag none

<IfModule mod_headers.c>
    Header unset ETag
    Header unset Cookie
    Header unset Set-Cookie
</IfModule>

<IfModule mod_rewrite.c>
    RewriteEngine On
</IfModule>

<IfModule mod_expires.c>
    ExpiresActive on
</IfModule>

<FilesMatch "\.(ico|jpg|jpeg|jpe|png|gif)$">
    ExpiresDefault "access plus 2 years"
</FilesMatch>

<IfModule mod_expires.c>
    ExpiresByType image/x-icon "access plus 2 years"
    ExpiresByType image/ico "access plus 2 years"
    ExpiresByType image/gif "access plus 2 years"
    ExpiresByType image/jpg "access plus 2 years"
    ExpiresByType image/jpe "access plus 2 years"
    ExpiresByType image/jpeg "access plus 2 years"
    ExpiresByType image/png "access plus 2 years"
</IfModule>

But when i visit www.example.com i don't want this to be set

1 Answers1

2

On Apache 2.4+ you can try putting the whole block of directives in an <If> block using an Apache Expression that tests the requested hostname.

For example:

<If "%{HTTP_HOST} == 'www-static.example.com'">

# :
# Directives go here...
# :

</If>

A few notes on your existing directives:

<IfModule mod_headers.c>
    Header unset ETag
    Header unset Cookie
    Header unset Set-Cookie
</IfModule>

There wouldn't seem to be any need to wrap all your blocks in <IfModule> wrappers.

Cookie is a request header, so you would need to use the RequestHeader directive instead, not Header (which applies to response headers). However, cookies should not have been set on this domain to begin with, so this should be unnecessary (and defeats the point of having a cookie-less domain in the first place). Likewise, unsetting the Set-Cookie should be unnecessary, since it shouldn't have been set in the first place.

<IfModule mod_rewrite.c>
    RewriteEngine On
</IfModule>

Unless your intention is to override mod_rewrite directives in a parent config then this would seem unnecessary?

<IfModule mod_expires.c>
    ExpiresActive on
</IfModule>

<FilesMatch "\.(ico|jpg|jpeg|jpe|png|gif)$">
    ExpiresDefault "access plus 2 years"
</FilesMatch>

<IfModule mod_expires.c>
    ExpiresByType image/x-icon "access plus 2 years"
    ExpiresByType image/ico "access plus 2 years"
    ExpiresByType image/gif "access plus 2 years"
    ExpiresByType image/jpg "access plus 2 years"
    ExpiresByType image/jpe "access plus 2 years"
    ExpiresByType image/jpeg "access plus 2 years"
    ExpiresByType image/png "access plus 2 years"
</IfModule>

You have mod_expires directives both inside and outside of <IfModule> containers - which doesn't really make sense. Plus you have an ExpiresDefault directive for file extensions which are already covered by the more specific ExpiresByType directives. ExpiresDefault should be used as a default for any other static resources that are served from this host.

Incidentally, the correct mime-type for jpg/jpeg/jpe files is image/jpeg. So, the other two related mime-types (image/jpg and image/jpe) are redundant - your server will only return one of these anyway, so you can double check by looking at the HTTP response your server is sending back.

So, your mod_expires directives should be written something like:

<IfModule mod_expires.c>
    ExpiresActive on
    ExpiresByType image/x-icon "access plus 2 years"
    ExpiresByType image/ico "access plus 2 years"
    ExpiresByType image/gif "access plus 2 years"
    ExpiresByType image/jpeg "access plus 2 years"
    ExpiresByType image/png "access plus 2 years"
    ExpiresDefault "access plus 2 years"
</IfModule>

Mind you, if all static resources are going to be "access plus 2 years", then you only strictly need the ExpiresDefault directive.

MrWhite
  • 11,643
  • 4
  • 25
  • 40