档案

Posts Tagged ‘Apache’

Apache .htaccess 常用配置代码

2011-07-27 留下评论

#重定向为小写字符串
RewriteMap  lc int:tolower
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule (.*) ${lc:$1} [R=301,L]

#重定向 favicon.ico 和 favicon.gif
RedirectMatch 301 favicon\.ico http://zhuohang.tk/favicon.ico
RedirectMatch 301 favicon\.gif http://zhuohang.tk/favicon.ico

#定义404错误页
ErrorDocument 404 errors/404.html

#设置环境变量
SetEnv APPLICATION_ENV development

#设置对应文件类型的缓存时间,提高网站访问速度
#1 按某个时间点设置缓存时间
<FilesMatch “\.(ico|pdf|flv|jpg|jpeg|png|gif|swf|mp3|mp4)$”>
Header set Cache-Control “public”
Header set Expires “Fri, 23 September 2011 20:00:00 GMT”
Header unset Last-Modified
</FilesMatch>

#2 html,htm,xml,txt,xsl类的文件缓存7200秒
<FilesMatch “\.(html|htm|xml|txt|xsl)$”>
Header set Cache-Control “max-age=7200, must-revalidate”
</FilesMatch>

#3 由DEFLATE过滤器实现输出压缩,并按时间点设置缓存时间
<FilesMatch “\.(js|css)$”>
SetOutputFilter DEFLATE
Header set Expires “Fri, 23 September 2011 20:00:00 GMT”
</FilesMatch>

#使用 GZIP 压缩输出,需要服务器支持 GZIP 模块
<IfModule mod_gzip.c>
mod_gzip_on       Yes
mod_gzip_dechunk  Yes
mod_gzip_item_include file      \.(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler   ^cgi-script$
mod_gzip_item_include mime      ^text/.*
mod_gzip_item_include mime      ^application/x-javascript.*
mod_gzip_item_exclude mime      ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</IfModule>

#跳转 不带www的URL 为 www开头的URL
RewriteEngine On
RewriteCond %{HTTP_HOST} ^viralpatel\.net$
RewriteRule (.*) http://www.viralpatel.net/$1 [R=301,L]

#自动在头部加入字符集与语言
AddDefaultCharset UTF-8
DefaultLanguage ens-ES

#发送自定义头部
Header set X-Pingback “http://www.askapache.com/xmlrpc.php&#8221;
Header set Content-Language “es-ES”

分类:Apache 标签:,

Apache副域名自动跳转到主域名的.htaccess脚本

2011-07-27 留下评论

# 副域名自动跳转到主域名
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?副域名1\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^(www\.)?副域名2\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^(www\.)?副域名3\.com [NC,OR]
.
.
.
RewriteRule (.*) http://主域名.com/$1 [R=301,L]
# END 副域名自动跳转到主域名

分类:Apache 标签:, , ,

10段有用的.htaccess代码

2011-07-15 留下评论

[代码] 移除url中的www

RewriteEngine On
RewriteCond %{HTTP_HOST} !^your-site.com$ [NC]
RewriteRule ^(.*)$ http://your-site.com/$1 [L,R=301]

Source: http://css-tricks.com/snippets/htaccess/www-no-www/

[代码] 防止热链

RewriteEngine On
#Replace ?mysite\.com/ with your blog url
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?mysite\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^$
#Replace /images/nohotlink.jpg with your "don't hotlink" image url
RewriteRule .*\.(jpe?g|gif|bmp|png)$ /images/nohotlink.jpg [L]

[代码] 重定向所有的WordPress feeds 到 feedburner

<IfModule mod_alias.c>
 RedirectMatch 301 /feed/(atom|rdf|rss|rss2)/?$ http://feedburner.com/yourfeed/
 RedirectMatch 301 /comments/feed/(atom|rdf|rss|rss2)/?$ http://feedburner.com/yourfeed/
</IfModule>
Source: http://www.wprecipes.com/how-to-redirect-wordpress-rss-feeds-to-feedburner-with-htaccess

[代码] 创建常规错误页面

ErrorDocument 400 /errors/badrequest.html
ErrorDocument 401 /errors/authreqd.html
ErrorDocument 403 /errors/forbid.html
ErrorDocument 404 /errors/notfound.html
ErrorDocument 500 /errors/serverr.html

Source: http://css-tricks.com/snippets/htaccess/custom-error-pages/

[代码] 强迫指定文件下载

<Files *.xls>
  ForceType application/octet-stream
  Header set Content-Disposition attachment
</Files>
<Files *.eps>
  ForceType application/octet-stream
  Header set Content-Disposition attachment
</Files>

Source: http://www.givegoodweb.com/post/30/forcing-a-download-with-apache-and-htaccess

[代码] PHP 错误日志

# display no errs to user
php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off
# log to file
php_flag log_errors on
php_value error_log /location/to/php_error.log

Source: http://css-tricks.com/snippets/htaccess/php-error-logging/

[代码] 从URL中移除文件拓展名,如.html

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
# Replace html with your file extension, eg: php, htm, asp

Source: http://eisabainyo.net/weblog/2007/08/19/removing-file-extension-via-htaccess

[代码] 防止自动目录列表

Options -Indexes

[代码] 压缩静态数据

AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/javascript text/css application/x-javascript

[代码] 强制指定特定的编码

<FilesMatch "\.(htm|html|css|js)$">
AddDefaultCharset UTF-8
</FilesMatch>

Source: http://www.askapache.com/htaccess/setting-charset-in-htaccess.html
分类:Apache, PHP 标签:,

Apache中.htaccess重定向某个类型的文件到另外的URL

2011-07-15 留下评论

RedirectMatch 301 (.*)\.asp$ http://www.domain.com/newpage.html

分类:Apache, PHP 标签:, , , ,