21个非常有用的.htaccess 提示和技巧
Apache Web 服务器可以通过 .htaccess 文件来操作各种信息,这是一个目录级配置文件的默认名称,允许去中央化的 Web 服务器配置管理。可用来重写服务器的全局配置。该文件的目的就是为了允许单独目录的访问控制配置,例如密码和内容访问。下面是 21 个非常有用的 .htaccess 配置的提示和技巧:
1. 定制目录的 Index 文件
DirectoryIndex index.html index.php index.htm
ErrorDocument 404 errors/404.html
ErrorDocument 404 /psych/cgi-bin/error/error?404
# no one gets in here! deny from all
# no nasty crackers in here! order deny,allow deny from all allow from 192.168.0.0/24 # this would do the same thing.. #allow from 192.168.0
# someone else giving the ruskies a bad name.. order allow,deny deny from 83.222.23.219 allow from all
SetEnv SITE_WEBMASTER "Jack Sprat" SetEnv SITE_WEBMASTER_URI mailto:Jack.Sprat@characterology.com UnSetEnv REMOTE_ADDR
Redirect 301 /old/file.html http://yourdomain.com/new/file.html
RedirectMatch 301 /blog(.*) http://yourdomain.com/$1
# year <FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|swf|mp3|mp4)$"> Header set Cache-Control "public" Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT" Header unset Last-Modified </FilesMatch> #2 hours <FilesMatch "\.(html|htm|xml|txt|xsl)$"> Header set Cache-Control "max-age=7200, must-revalidate" </FilesMatch> <FilesMatch "\.(js|css)$"> SetOutputFilter DEFLATE Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT" </FilesMatch>
<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>
<Location> SetOutputFilter DEFLATE SetEnvIfNoCase Request_URI \ \.(?:gif|jpe?g|png)$ no-gzip dont-vary SetEnvIfNoCase Request_URI \ \.(?:exe|t?gz|zip|gz2|sit|rar)$ no-gzip dont-vary </Location>
<FilesMatch "\.(txt|html|htm|php)"> php_value output_handler ob_gzhandler </FilesMatch>
RewriteEngine On RewriteCond %{HTTPS} !on RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteEngine on RewriteRule ^product-([0-9]+)\.html$ product.php?id=$1
RewriteEngine on RewriteRule ^product/([a-zA-Z0-9_-]+)/([0-9]+)\.html$ product.php?id=$2
RewriteEngine On RewriteCond %{HTTP_HOST} ^viralpatel\.net$ RewriteRule (.*) http://www.viralpatel.net/$1 [R=301,L]
RewriteEngine On RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1 RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?username=$1
RewriteEngine On RewriteCond %{HTTP_HOST} ^test\.com$ [OR] RewriteCond %{HTTP_HOST} ^www\.test\.com$ RewriteCond %{REQUEST_URI} !^/new/ RewriteRule (.*) /new/$1
Options -Indexes
IndexIgnore *
AddType application/x-endnote-connection enz AddType application/x-endnote-filter enf AddType application/x-spss-savefile sav
RewriteCond %{HTTP_REFERER} !^$ RewriteCond %{REQUEST_URI} !^/(wp-login.php|wp-admin/|wp-content/plugins/|wp-includes/).* [NC] RewriteCond %{HTTP_REFERER} !^http://www.askapache.com.*$ [NC] RewriteRule \.(ico|pdf|flv|jpg|jpeg|mp3|mpg|mp4|mov|wav|wmv|png|gif|swf|css|js)$ - [F,NS,L]
php_value upload_max_filesize 20M php_value post_max_size 20M php_value max_execution_time 200 php_value max_input_time 200
Options -ExecCGI AddHandler cgi-script .php .pl .py .jsp .asp .htm .shtml .sh .cgi
AddDefaultCharset UTF-8 DefaultLanguage en-US
SetEnv TZ America/Indianapolis
AddType application/octet-stream .avi .mpg .mov .pdf .xls .mp4
<Files quiz.html> order deny,allow deny from all AuthType Basic AuthName "Characterology Student Authcate" AuthLDAP on AuthLDAPServer ldap://directory.characterology.com/ AuthLDAPBase "ou=Student, o=Characterology University, c=au" require valid-user satisfy any </Files>
Header set Set-Cookie "language=%{lang}e; path=/;" env=lang
RewriteEngine On RewriteBase / RewriteRule ^(.*)(de|es|fr|it|ja|ru|en)/$ - [co=lang:$2:.yourserver.com:7200:/]
Header set P3P "policyref="http://www.askapache.com/w3c/p3p.xml"" Header set X-Pingback "http://www.askapache.com/xmlrpc.php" Header set Content-Language "en-US" Header set Vary "Accept-Encoding"
SetEnvIfNoCase ^User-Agent$ .*(craftbot|download|extract|stripper|sucker|ninja|clshttp|webspider|leacher|collector|grabber|webpictures) HTTP_SAFE_BADBOT SetEnvIfNoCase ^User-Agent$ .*(libwww-perl|aesop_com_spiderman) HTTP_SAFE_BADBOT Deny from env=HTTP_SAFE_BADBOT