什么是 .htaccess 文件? 概述来讲,htaccess 文件是 Apache 服务器中的一个配置文件,它负责相关目录下的网页配置。 经过 htaccess 文件,能够帮咱们实现:网页301重定向、自定义404错误页面、改变文件扩展名、容许/阻止特定的用户或者目录的访问、禁止目录列表、配置默认文档等功能。javascript
理解 WordPress 的 htaccessphp
|
# BEGIN WordPress #这是一行注释,表示 WordPress 的 htaccess 从这里开始
<
IfModule
mod_rewrite
.
c
>
#若是 Apache 加载了 mod_rewrite.c 模块,则运行如下代码
RewriteEngine
On
#启用 mod_rewrite 引擎
RewriteBase
/
#设置目录重写的基准URL为 /
RewriteRule
^
index
\
.
php
$
-
[
L
]
#若是请求路径是 index.php,中止重写操做(避免死循环)
RewriteCond
%
{
REQUEST_FILENAME
}
!
-
f
#若是请求的不是一个文件,继续处理
RewriteCond
%
{
REQUEST_FILENAME
}
!
-
d
#若是请求的不是一个目录,继续处理
RewriteRule
.
/
index
.
php
[
L
]
#把全部的请求指向 /index.php
<
/
IfModule
>
#结束 IfModule
# END WordPress #WordPress 的 htaccess 到这里结束
|
使用范例 设置错误页面html
|
ErrorDocument
400
/
error_pages
/
400.html
ErrorDocument
401
/
error_pages
/
401.html
ErrorDocument
403
/
error_pages
/
403.html
ErrorDocument
404
/
error_pages
/
404.html
ErrorDocument
500
/
error_pages
/
500.html
|
设置重定向java
|
#从 old_dir 目录重定向到 new_dir 目录
Redirect
/
old_dir
/
http
:
//www.yourdomain.com/new_dir/index.html
#把经过二级目录访问的请求301重定向到二级域名
RedirectMatch
301
/
dir
/
(
.
*
)
http
:
//dir.yourdomain.com/$1
|
禁止指定IP访问git
|
#禁止 IP 为 255.0.0.0 和 123.45.6.区段的 IP 访问
order
allow
,
deny
deny
from
255.0.0.0
deny
from
123.45.6.
allow
from
all
|
禁止指定来源访问github
|
#禁止从 otherdomain.com 和 anotherdomain.com 的来源访问
RewriteEngine
on
# Options +FollowSymlinks
RewriteCond
%
{
HTTP_REFERER
}
otherdomain
\
.
com
[
NC
,
OR
]
RewriteCond
%
{
HTTP_REFERER
}
anotherdomain
\
.
com
RewriteRule
.
*
-
[
F
]
|
文件防盗链web
|
#从本站之外的域名访问图片,一概显示 feed.jpg
RewriteEngine
on
RewriteCond
%
{
HTTP_REFERER
}
!
^
$
RewriteCond
%
{
HTTP_REFERER
}
!
^
http
:
//(www\.)?yourdomain.com/.*$ [NC]
RewriteRule
\
.
(
gif
|
jpg
|
png
)
$
http
:
//www.yourdomain.com/feed.jpg [R,L]
|
禁用文件夹列表apache
|
#若是你的文件夹没有首页文件,服务器会显示文件列表,你能够设置不显示
IndexIgnore
*
#仅不显示 .zip/.jpg/.gif 格式的文件
IndexIgnore
*
.
zip
*
.
jpg
*
.
gif
|
设置文件夹首页服务器
|
#防止显示文件夹列表,当访问文件夹时,服务器查找 index.html 为首页文件,如不存在依次向后查找
DirectoryIndex
index
.
html
index
.
cgi
index
.
php
|
设置媒体文件为可下载的而非播放app
|
AddType
application
/
octet
-
stream
.
mp3
.
mp4
|
自定义 HTTP 报头
|
Header
set
X
-
Pingback
"http://www.yourdomain.com/xmlrpc.php"
Header
set
article
-
by
"yu1u.org"
|
参考资料