二十四则Apache Rewrite规则

1、防盗链

Options +FollowSymlinks
#Protect against hotlinking
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?domainname.com/ [nc]
RewriteRule .*.(gif|jpg|png)$ http://domainname.com/img/stop_stealing_bandwidth.gif[nc]

2、通过User Agents 禁止访问

#Block bad bots
SetEnvIfNoCase user-Agent ^FrontPage [NC,OR]
SetEnvIfNoCase user-Agent ^Java.* [NC,OR]
SetEnvIfNoCase user-Agent ^Microsoft.URL [NC,OR]
SetEnvIfNoCase user-Agent ^MSFrontPage [NC,OR]
SetEnvIfNoCase user-Agent ^Offline.Explorer [NC,OR]
SetEnvIfNoCase user-Agent ^[Ww]eb[Bb]andit [NC,OR]
SetEnvIfNoCase user-Agent ^Zeus [NC]
<limit get=”” post=”” head=””>
Order Allow,Deny
Allow from all
Deny from env=bad_bot
</limit>

3、重定向(禁止)所有访问,某些来源IP除外

ErrorDocument 403 http://www.domainname.com
Order deny,allow
Deny from all
Allow from 124.34.48.165
Allow from 102.54.68.123

4、SEO 友好 301 重定向

Redirect 301 /d/file.html http://www.domainname.com/r/file.html

5、自定义错误页面

ErrorDocument 401 /error/401.php
ErrorDocument 403 /error/403.php
ErrorDocument 404 /error/404.php
ErrorDocument 500 /error/500.php

6、禁止某些来源IP

allow from all
deny from 145.186.14.122
deny from 124.15

7、设置服务器管理员的默认邮箱地址

ServerSignature EMail
SetEnv SERVER_ADMIN default@domain.com

8、禁用显示下载请求

 

AddType application/octet-stream .pdf
AddType application/octet-stream .zip
AddType application/octet-stream .mov

 

9、保护特定的文件,禁止访问

 

#Protect the .htaccess File
<files .htaccess=””>
order allow,deny
deny from all
</files>

10、利用mod_deflate压缩文件

<ifmodule mod_deflate.c=””>
<filesmatch .(js|css)$=””>
SetOutputFilter DEFLATE
</filesmatch>
</ifmodule>

11、添加Expires头

<filesmatch .(ico|pdf|flv|jpg|jpeg|png|gif|swf)$=””>
Header set Expires “Wed, 21 May 2010 20:00:00 GMT”
</filesmatch>

12、设置默认页

#Serve Alternate Default Index Page
DirectoryIndex about.html

13、设置密码保护文件和目录

 

#password-protect a file
<files secure.php=””>
AuthType Basic
AuthName “Prompt”
AuthUserFile /home/path/.htpasswd
Require valid-user
</files>
# password-protect a directory
resides
AuthType basic
AuthName “This directory is protected”
AuthUserFile /home/path/.htpasswd
AuthGroupFile /dev/null
Require valid-user

14、将老域名重定向新域名

 

#Redirect from an old domain to a new domain
RewriteEngine On
RewriteRule ^(.*)$ http://www.domainname.com/$1 [R=301,L]

15、强制缓存

FileETag MTime Size
ExpiresActive on
ExpiresDefault “access plus 86400 seconds”

16、启用GZIP压缩相关文件

AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/javascript text/css application/x-javascript
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch bMSIE !no-gzip !gzip-only-text/html

17、从URL中移除“category”

RewriteRule ^category/(.+)$ http://www.yourdomain.com/$1 [R=301,L]

18、禁止目录浏览

Options All -Indexes

19、重定向WordPress的订阅到Feedburner

#Redirect wordpress content feeds to feedburner
<ifmodule mod_rewrite.c=””>
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} !FeedBurner    [NC]
RewriteCond %{HTTP_USER_AGENT} !FeedValidator [NC]
RewriteRule ^feed/?([_0-9a-z-]+)?/?$ http://feeds.feedburner.com/yourfeed [R=302,NC,L]
</ifmodule>

20、禁止空Referrer访问内容

RewriteEngine On
RewriteCond %{REQUEST_METHOD} POST
RewriteCond %{REQUEST_URI} .wp-comments-post\.php*
RewriteCond %{HTTP_REFERER} !.*yourblog.com.* [OR]
RewriteCond %{HTTP_USER_AGENT} ^$
RewriteRule (.*) ^http://%{REMOTE_ADDR}/$ [R=301,L]

21、从URL中删除文件扩展名

RewriteRule ^(([^/]+/)*[^.]+)$ /$1.php [L]

22、将www.domain.com重定向到domain.com

#remove www from URI
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]

 

23、URL结尾添加/

 

#trailing slash enforcement
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !#
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://domain.com/$1/ [L,R=301

24、将www.domain.com/xxx重定向到domain.com/xxx

# Redirect if www.yourdomain.com to yourdomain.com
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule (.*) http://example.com/$1 [R=301,L]

from http://hi.baidu.com/cy594/blog/item/4b29367a67c1c4fa2e73b3db.html




coded by nessus
发表评论?

0 条评论。

发表评论