Apache .htaccess 常用配置代码
#重定向为小写字符串
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”
Header set Content-Language “es-ES”
Apache副域名自动跳转到主域名的.htaccess脚本
# 副域名自动跳转到主域名
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 副域名自动跳转到主域名
域名绑定子目录时隐藏路径的方法
一般我们在设置Hostmonster主机多域名绑定一个站点时,往往会在浏览器中出现子目录文件名。这样会让人们浏览到我们创建的子目录,如果我们不想让它出现,我们需要做一下转向设置,隐藏该子目录。首先需要在子目录中创建htaccess文件,然后上传以下代码:
Rewrite实现301转向
例如要将http://www.zhuohang.tk/bbs 转向到 http://bbs.zhuohang.tk 可以用: redirect 301 /bbs http://bbs.zhuohang.tk
或者
RewriteEngine On
RewriteRule ^(.*)$ http://bbs.zhuohang.tk/$1 [R=301,L]
上面的代码就可以将 http://www.zhuohang.tk/bbs/forum-51-1.html 这样类型的url转向到 http://bbs.zhuohang.tk/forum-51-1.html
补充说明:主域名指向子目录时使用以上方法不能实现隐藏子目录
理由:主域名绑定根目录,如果指向子目录需要设置转向功能。例如,使用以下代码进行设置:
RewriteEngine on
#更改yourmaindomain.com为你的主域名地址
RewriteCond %{HTTP_HOST} ^(www.)?yourmaindomain.com$
# 更改主域名要指向的子目录
RewriteCond %{REQUEST_URI} !^/subfolder/
#不要更改这两行
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# 更改主域名要指向的子目录
RewriteRule ^(.*)$ /subfolder/$1
#更改yourmaindomain.com为你的主域名地址
RewriteCond %{HTTP_HOST} ^(www.)?yourmaindomain.com$
#更改index.php为你的网站首页文件
RewriteRule ^(/)?$ subfolder/index.php [L]
在上述代码中实现了主域名指向子目录,使用了转向功能,如果再使用301转向设置隐藏子目录文件名。会造成循环转向。
PHP 生成 .htpasswd 文件的用户名和密码
<?php
// – – – – – – – – – – – – – – – – – – –
// Author: Christopher Hair
// Website: http://www.chrishair.co.uk
// – – – – – – – – – – – – – – – – – – –
if (isset($_POST[‘submit’])){
$error = ”;
$username = trim(stripslashes($_POST[‘username’]));
$password = trim(stripslashes($_POST[‘password’]));
if ($username == ” || preg_match(‘/\s/m’, $username)) {
$error .= “<p><strong>Invalid Username.</strong></p>\n”;
}
if ($password == ” || preg_match(‘/\s/m’, $password)) {
$error .= “<p><strong>Invalid Password.</strong></p>\n”;
}
if ($error == ”) {
echo “<p>Success</p>\n”;
echo “<h1>” . htmlspecialchars($username) . “:” . crypt($password) . “</h1>\n”;
} else {
echo $error;
}
}
?>
替换 .htaccess 为 WordPress 提速
# BEGIN WordPress
RewriteEngine on
#
# Unless you have set a different RewriteBase preceding this
# point, you may delete or comment-out the following
# RewriteBase directive:
RewriteBase /
#
# if this request is for “/” or has already been rewritten to WP
RewriteCond $1 ^(index\.php)?$ [OR]
# or if request is for image, css, or js file
RewriteCond $1 \.(gif|jpg|css|js|ico)$ [NC,OR]
# or if URL resolves to existing file
RewriteCond %{REQUEST_FILENAME} -f [OR]
# or if URL resolves to existing directory
RewriteCond %{REQUEST_FILENAME} -d
# then skip the rewrite to WP
RewriteRule ^(.*)$ – [S=1]
# else rewrite the request to WP
RewriteRule . /index.php [L]
#
# END wordpress
10段有用的.htaccess代码
[代码] 移除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中.htaccess重定向某个类型的文件到另外的URL
RedirectMatch 301 (.*)\.asp$ http://www.domain.com/newpage.html