Posts tagged .htaccess

从A到Z!26个用于 WordPress 的 .htaccess 规则

作者Ligyxy

我们知道 .htaccess 文件能轻松地强化你的博客,减少带宽并增加可用性。前面曾经写过一篇简易的 .htaccess 教程:《简单实用的.htaccess小技巧》。如果你还想继续了解 .htaccess 文件,那么不妨看看下面这26个规则:

但是请先记住一条黄金法则:

先备份,再折腾!

A – WP- Admin – 管理员页面

你可以限制访问 WP- Admin 页面的 IP 地址

1

2

3

order deny,allow

allow from a.b.c.d # This is your static IP

deny from all

来源 -BlogSecurity.net

B – Blacklist – 黑名单

.htaccess 的一个重要功能是你能把它作为 IP 黑名单来使用:

1

2

3

4

5

<Limit GET POST PUT>

order allow,deny

allow from all

deny from 123.456.789

</LIMIT>

来源 – Perishable Press

C – WP-Config Protection – 保护 Config

WP-Config 文件里包含了你的数据库名、数据库用户名与密码。这样的文件你敢让别人看到吗?所以你会需要用 .htaccess 来保护它。

1

2

3

4

5

# protect wpconfig.php

<files wp-config.php>

order allow,deny

deny from all

</files>

来源 – Josiah Cole

D – Disable Directory Browsing – 禁止浏览目录

WP-Config 文件里包含了你的数据库名、数据库用户名与密码。这样的文件你敢让别人看到吗?所以你会需要用 .htaccess 来保护它。

1

2

# disable directory browsing

Options All -Indexes

来源Josiah Cole

E – Explanation – 释疑

如果我问你 .htaccess 是什么,我猜你一定很难说明白。老实说,我自个儿也不明白。好在维基百科给了个明确的解释:

.htaccess Apache HTTP Server的文件目录系统级别的配置文件的默认的名字。它提供了在主配置文件中定义用户自定义指令的支持。

维基百科也给出了一些特定的例子,你可以访问:http://zh.wikipedia.org/w/index.php?title=.htaccess&variant=zh-cn 了解更多。

F – Feedburner – RSS烧制

Feedburner 是各位博主的好帮手,通过这个例子你能把原来的 RSS 地址转向到烧制后的地址。

1

2

3

4

5

6

7

# temp 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/perishablepress [R=302,NC,L]

</IfModule>

来源 – Perishable Press

G – Get an RSS Feed on a static page – 获取静态页的RSS

通过下面这个链接,你能学习到利用 .htaccess 获取静态页 RSS 的办法。

链接 – adityaspeaks.com

H – Disable hotlinking – 防止盗链

所谓盗链,就是别人私自用了你服务器上的图片、声音等文件,占用了你的带宽。你可以通过下面这个 .htaccess 规则来阻止盗链:

1

2

3

4

5

6

#disable hotlinking of images with forbidden or custom image option

RewriteEngine on

RewriteCond %{HTTP_REFERER}
!^$

RewriteCond %{HTTP_REFERER}
!^http://(www\.)?yourdomain.com/.*$ [NC]

#RewriteRule \.(gif|jpg)$ -
[F]

#RewriteRule \.(gif|jpg)$ http://www.yourdomain.com/stealingisbad.gif [R,L]

来源 – Josiah Cole

I – Important! – 重要提示!

是啊,呵呵,“I”开头的词是有些难找,但是它同样重要,very Important!

备份,要记住备份。当你把东西搞得一团糟时,只有备份文件能救你!

J – Jauntily show the admin’s email address in error message

*抱歉,本词条有误*

K – Keep RSS ‘content thieves’ away – 防范“RSS小偷

你一定不希望有人窃取你网站的内容吧?他们只要有你的 RSS 地址就能复制内容。如果你有对方网站的IP地址(怎么获取?很简单,google 它),就能屏蔽该网站对你的 RSS 的读取。如果有不止一家网站复制你的内容,你只要增加 IP 地址就行了。

1

2

3

RewriteEngine on

RewriteCond %{REMOTE_ADDR}
^69.16.226.12

RewriteRule ^(.*)$ http://newfeedurl.com/feed

来源 – Seo Black Hat

L – Limiting number of simultaneous connections – 限制并发连接数

如果你要限制并发连接数(就是同时访问你的网站的人数),就使用下面这个代码。

1

MaxClients < number-of-connections>

M – Maintenance – 制作临时维护页面

无论什么原因,可能是维护、更新,你都可能会要暂时停止你的网站。这时你就需要一个维护页。无论访客访问的是你网站的任一 URL 还是 IP 地址,都能转向到维护页面。

1

2

3

4

RewriteEngine on

RewriteCond %{REQUEST_URI}
!/maintenance.html$

RewriteCond %{REMOTE_ADDR}
!^123\.123\.123\.123

RewriteRule $ /maintenance.html
[R=302,L]

来源 – CatsWhoCode/Woueb.net

N – Deny no referer requests [stop spam comments!] – 拒绝垃圾留言!

这是一个比下文“S”词条更简便的防 Spam 方法。其原理是,如果留言者不公开来源,即是用机器留言的,就屏蔽之。就这么简单。

1

2

3

4

5

6

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]

来源 – WPRecipes

O – Force files when opening to ‘save as’ – 另存为方式打开文档

有时候你空间里的一些文件,比如音乐、视频文件,点开后会有软件自动打开这个文件。如果你不想自动打开,这个规则能把文件强制为另存为方式下载。

1

AddType application/octet-stream .avi .mpg .mov .pdf .xls .mp4

来源 – AskApache

P – Protect your .htaccess file – 保护 .htaccess 文件

如果你的博客已经做好的十足的防护措施,是不是也要考虑一下保护你的 .htaccess 文件呢?黑客也可能通过这个文件发起攻击。下面这个规则能禁止下载 .htaccess 文件。

1

2

3

4

5

6

# STRONG HTACCESS PROTECTION

<Files ~ “^.*\.([Hh][Tt][Aa])”>

order allow,deny

deny from all

satisfy all

</Files>

来源Perishable Press

Q – Quicken your site’s loading time by caching – 设置缓存

下面这个链接有详细的教程,教你如何给 WordPress 博客设置缓存。

链接 – Samaxes

R – Redirect to other pages on your site – 301重定向

1

RedirectMatch 301
^/blog/.*$ http://domain.tld/target.html

来源 – Perishable Press

S - Spam! – 阻止垃圾留言

通过 .htaccess 来禁止 Spam 是个好办法。下面这个链接给出了一个 Spam 黑名单,复制到你的 .htaccess 里,你就能防止绝大部分的垃圾留言。

来源perishablepress.com

T – Set the Timezone of the server – 设置服务器时区

译者:代码给出的是把TZ值设置为美国时间的代码,如果服务器在中国,你可以改为Asia/Shanghai

1

SetEnv TZ America/Indianapolis

来源 – AskApache

U – Remove /category/ from your category URL – 简化 WP 分类页面地址

WordPress 的分类页面地址里有一个 /category/ ,会不会觉得这很多余呢?下面这个代码就能去掉它!

1

RedirectMatch 301
^/category/(.+)$ http://www.askapache.com/$1

或者

1

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

来源AskApache

V – Valiantly automatically fix URL spelling mistakes – 修复错误地址

这个代码能自动检查英文地址的拼写错误。

1

2

3

<IfModule mod_speling.c>

CheckSpelling On

</IfModule>

来源 – Vortex Mind

W – Redirect from http://Www.whatever to http://Whatever – 去掉网址前面的www.

使用301转跳,实现网址标准化。

1

2

3

4

5

# permanently redirect from www domain to non-www domain

RewriteEngine on

Options +FollowSymLinks

RewriteCond %{HTTP_HOST}
^www\.domain\.tld$ [NC]

RewriteRule ^(.*)$ http://domain.tld/$1 [R=301,L]

来源Stupid htaccess tricks

X – Make your wp-login.php page Xenophobic – 限制他人访问 wp-login

Xenophobic: “排外,仇视陌生人

我觉得你应该适当使用这个规则。如果你不是多人博客,可以限制他人访问登录页面,以达到更高的安全性。

1

2

3

4

5

<Files wp-login.php>

Order deny,allow

Deny from All

Allow from 123.456.789.0

</Files>

来源 – Reaper-X

Y – Easily rename your .htaccess file – 重命名 .htaccess 文件

如果你的服务器不接受以句点开头的文件怎么办?重命名它呗!下面这个代码能实现 .htaccess 文件的重命名。

1

2

# rename htaccess files

<code>AccessFileName ht.access

来源 – Perishable Press

Z – Say Zygote in your .htaccess file – .htaccess 里写注释

你可能会想在 .htaccess 文件里做些解释,好让别人能看懂。那怎么做注释呢?请看下面的代码:

1

# see -
this
is a comment - you can only use letters and numbers and - and _ That is why there are no commas

要注意的是注释里只能写字母、数字与下划线

翻译:所以说

原文:wpshout.com

译者后记:原文的作者看样子也是经过收集才写成这篇文章的。如果你看了原文,请你注意,原文的有些代码里有一些多余或错误代码。我在本译文里都已做了修正。

From: http://www.suoyishuo.com/archives/a-to-z-of-wordpress-htaccess-hacks.html

简单实用的.htaccess小技巧

.htaccess 文件 (Hypertext Access file) 是Apache Web服务器的一个非常强大的配置文件,对于这个文件,Apache有一堆参数可以让你配置出几乎随心所欲的功能。.htaccess 配置文件坚持了Unix的一个文化——使用一个ASCII 的纯文本文件来配置你的网站的访问策略。

这篇文章包括了16个非常有用的小技巧。另外,因为.htaccess 是一个相当强大的配置文件,所以,一个轻微的语法错误会造成你整个网站的故障,所以,在你修改或是替换原有的文件时,一定要备份旧的文件,以便出现问题的时候可以方便的恢复。

1. 使用.htaccess 创建自定义的出错页面。对于Linux Apache来说这是一项极其简单的事情。使用下面的.htaccess语法你可以轻松的完成这一功能。(把.htaccess放在你的网站根目录下)

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

2. 设置网站的时区

SetEnv TZ America/Houston

3. 阻止IP列表
有些时候,你需要以IP地址的方式阻止一些访问。无论是对于一个IP地址还是一个网段,这都是一件非常简单的事情,如下所示:

allow from all
deny from 145.186.14.122
deny from 124.15

Apache对于被拒绝的IP会返回403错误。

4. 把一些老的链接转到新的链接上——搜索引擎优化SEO

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

5. 为服务器管理员设置电子邮件。

ServerSignature EMail
SetEnv SERVER_ADMIN
default@domain.com

6. 使用.htaccess 访止盗链。如果你网站上的一个图片被别的N多的网站引用了,那么,这很有可能会导致你服务器的性能下降,使用下面的代码可以保护某些热门的链接不被过多的引用。

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

7. 阻止 User Agent 的所有请求

## .htaccess Code :: BEGIN
## Block Bad Bots by user-Agent
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]

Order Allow,Deny
Allow from all
Deny from env=bad_bot

## .htaccess Code :: END

8. 把某些特殊的IP地址的请求重定向到别的站点

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

9. 直接找开文件而不是下载 – 通常,我们打开网上文件的时候总是会出现一个对话框问我们是下载还是直接打开,使用下面的设置就不会出现这个问题了,直接打开。

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

10. 修改文件类型 – 下面的示例可以让任何的文件都成为PHP那么被服务器解释。比如:myphp, cgi,phtml等。

ForceType application/x-httpd-php
SetHandler application/x-httpd-php

11. 阻止存取.htaccess 文件

# secure htaccess file

order allow,deny
deny from all

12. 保护服务器上的文件被存取

# prevent access of a certain file order allow,deny
deny from all

13. 阻止目录浏览

# disable directory browsing
Options All -Indexes

14. 设置默认主页

# serve alternate default index page
DirectoryIndex about.html

15. 口令认证 – 你可以创建一个文件用于认证。下面是一个示例:

# to protect a file

AuthType Basic
AuthName “Prompt”
AuthUserFile /home/path/.htpasswd
Require valid-user

# password-protect a directory
resides
AuthType basic
AuthName “This directory is protected”
AuthUserFile /home/path/.htpasswd
AuthGroupFile /dev/null
Require valid-user

16. 把老的域名转像新的域名

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

文章:来源