大网数据广告活动
服务器运维问答

set htaccess condition if servername is www-static.example.com

· 52CE 我爱测

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 Fi…

问题原文

原帖链接 · Bas van den Dikkenberg · CC BY-SA 4.0

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

已采纳解答

原帖链接 · MrWhite · CC BY-SA 4.0

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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-11-14)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。