首先我们编辑apache的虚拟配置文件,
vim /usr/local/apache2/conf/extra/httpd-vhosts.conf,编辑或者添加这些内容
<VirtualHost *:80>
DocumentRoot"/data/www/"
ServerNamewww.test.com
ServerAlias www.hu.com
##设置这些格式的东西不记录到日志
SetEnvIfRequest_URI ".*\.gif$" image-request
SetEnvIfRequest_URI ".*\.jpg$" image-request
SetEnvIfRequest_URI ".*\.png$" image-request
SetEnvIfRequest_URI ".*\.bmp$" image-request
SetEnvIfRequest_URI ".*\.swf$" image-request
SetEnvIfRequest_URI ".*\.js$" image-request
SetEnvIfRequest_URI ".*\.css$" image-request
ErrorLog"logs/test.com-error_log"
CustomLog"|/usr/local/apache2/bin/rotatelogs -l/usr/local/apache2/logs/test.com-access_%Y%m%d_log 86400" combinedenv=!image-request ##(切割每天的日志文件格式是年-月-日_log)
##设置域名跳转
<IfModulemod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.hu.com$
RewriteRule ^/(.*)$ http://www.test.com/$1 [R=301,L]
##user_agent的设置
#rewritecond %{http_user_agent} ^.*curl.* [nc,or]
#rewritecond %{http_user_agent} ^.*chrome.* [nc]
#rewriterule .* - [F]
RewriteCond %{REQUEST_URI} ^.*/tmp/.* [NC]
rewriterule .* - [F]
</IfModule>
##设置网站的静态缓存
<IfModule mod_expires.c>
ExpiresActiveon
ExpiresByTypeimage/gif "access plus 1 days"
ExpiresByTypeimage/jpeg "access plus 24 hours"
ExpiresByTypeimage/png "access plus 24 hours"
ExpiresByTypetext/css "now plus 2 hours"
ExpiresByTypeapplication/x-javascript "now plus2 hours"
ExpiresByTypeapplication/x-shockwave-flash "now plus 2 hours"
ExpiresDefault "now plus 0 min"
</IfModule>
##设置防盗链
SetEnvIfNoCase Referer "^$" local_ref
SetEnvIfNoCase Referer"^http://.*\.test\.com" local_ref
SetEnvIfNoCase Referer ".*\.blog.51cto\.com"local_ref
<filesmatch"\.(txt|doc|mp3|zip|rar|jpg|gif|png|css|js)">
Order Allow,Deny
Allow from env=local_ref
</filesmatch>
##设置网站的访问控制
<Directory "/data/www">
AllowOverride None
Options None
Orderallow,deny
Allow fromall
</Directory>
<filesmatch "(.*)admin(.*)">
Orderdeny,allow
Deny fromall
deny from127.0.0.1
allow from192.168.75.1
</filesmatch>
</VirtualHost>