18个有用的 .htaccess 文件使用技巧

简介: .htaccess 是 Web 服务器 Apache 中特有的一个配置文件,操控着服务器上的许多行为,我们可以利用它来做许多事情,例如:设置访问权限,网址重定向,等等。本文向大家展示18条 .htaccess 文件的使用技巧,欢迎各位收藏备用。

.htaccess 是 Web 服务器 Apache 中特有的一个配置文件,操控着服务器上的许多行为,我们可以利用它来做许多事情,例如:设置访问权限,网址重定向,等等。本文向大家展示18条 .htaccess 文件的使用技巧,欢迎各位收藏备用。

温馨提示:在编辑 .htaccess 文件时,切记一定要先做好备份,因为这个文件相当重要,编辑错了可能会出现不可想像的后果!

1.去除博客网址中的 WWW.

据说这样做有利于 SEO。将下面的代码加入到 .htaccess 文件中,结果会把所有带 www. 的网址重定向到不带 www. 的网址。需要将 ijinfa.cn 改成您的域名哦!

?
1
2
3
RewriteEngine On
RewriteCond %{HTTP_HOST} !^ijinfa.cn$ [NC]
RewriteRule ^(.*)$ http: //ijinfa.cn/$1 [L,R=301]

来源: css-tricks

2.强制使用WWW.格式的网址

将 www.ijinfa.cn 改成您的域名哦!

?
1
2
3
RewriteEngine On
RewriteCond %{HTTP_HOST} ^ijinfa.cn [NC]
RewriteRule ^(.*)$ http: //www.ijinfa.cn/$1 [L,R=301]

来源: css-tricks

3.设置图片防盗链

?
1
2
3
4
5
6
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]

4.重定向 WordPress 博客中的所有 feeds 到 feedburner

将 http://feedburner.com/yourfeed/ 换成您的 feedburner ID 号。

?
1
2
3
4
<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>

来源: wprecipes

5.自定义错误页面

需要您自己创建一些 html 文件,访问者访问不存在的页面时就会显示这些 html 页面。

?
1
2
3
4
5
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

来源: css-tricks

6.强制下载特定的文件

如果您的网站提供 mp3s, eps 或 xls 此类文件的下载,要是不想让浏览器来决定是否下载,利用如下代码可强制下载。

?
1
2
3
4
5
6
7
8
<Files *.xls>
   ForceType application/octet-stream
   Header set Content-Disposition attachment
</Files>
<Files *.eps>
   ForceType application/octet-stream
   Header set Content-Disposition attachment
</Files>

来源: givegoodweb

7.PHP 错误记录

在您的服务器上创建一个 php_error.log 文件,再将以下代码放入 .htaccess 文件中,记得修改第七行中的文件路径。这是一条很有用的函数。

?
1
2
3
4
5
6
7
# 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

来源: css-tricks

8.从网址中去除文件的扩展名

文件扩展名对网页开发者很有用,但对访问的人没用,您也不希望他们看到的,使用下面的方法将它们去掉。下面是去掉 html 扩展名的方法,可以改成 php, htm, asp 等。

?
1
2
3
4
5
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.<span style= "color: #ff0000;" >html</span> -f
RewriteRule ^(.*)$ $1 .<span style= "color: #ff0000;" >html</span>
# Replace html with your file extension, eg: php, htm, asp

来源:eisabainyo

9.禁止自动列出系统文件目录

如果您网站某个文件目录不存在 index 文件,服务器会自动列出当前目录中的所有文件,通常您并不希望让别人看到,使用下面这句代码,可以禁止目录被列出。

?
1
Options -Indexes

10.压缩静态文件,加速页面下载

这样会在一定程度上减少服务器带宽消耗,加快读者访问速度。

?
1
2
3
4
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

11.给文件自动添加utf-8编码

对于某些特定的文件,如 html, htm,css 或 js 等,有时可能会忘记在页面中加上一条 <meta http-equiv=”Content-Type”>  编码标签,为防止出现编码问题,可让页面自动生成这样的标签。

?
1
2
3
<FilesMatch "\.(htm|html|css|js)$" >
ForceType 'text/html; charset=UTF-8'
</FilesMatch>

来源: askapache

12.去除 WordPress 网址中的 /category/ 目录名称

默认情况下, WordPress 分类目录的永久链接会显示如下:

?
1
http: //www.catswhocode.com/blog/category/wordpress

其中的目录名 /category/ 毫无用处,可以去掉,只需在 .htaccess 文件中加上下面这一行(其中http://www.yourblog.com/ 是您的域名):

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

将修改后的文件保存,分类目录的链接形式就会变成如下:

?
1
http: //www.yourblog.com/blog/wordpress

来源: wprecipes

13.使用浏览器缓存

一个有效加快博客速度的方法就是,强制浏览器缓存。在 .htaccess 文件中添加以下语句:

?
1
2
3
4
5
6
7
FileETag MTime Size
<ifmodule mod_expires.c>
   <filesmatch "\.(jpg|gif|png|css|js)$" >
        ExpiresActive on
        ExpiresDefault "access plus 1 year"
    </filesmatch>
</ifmodule>

来源: wordpress-tutoriel

14.将带发表日期和日志名称的永久链接重定向到 /%postname%/ 格式

这个 /%postname%/ 就是日志名称,也就是使用日志名称作为永久链接。首先登录 WordPress 后台,到设置→ 永久链接,在自定义永久链接这一项中填上
/%postname%/,之后您的博客永久链接会变成如下这样:

?
1
http: //www.yourblog.com/name-of-the-post

接着下来重要的一步,利用301重定向将所有旧的链接全部指向上面所建新链接。打开 WordPress 根目录下的 .htaccess 文件,加入以下一行语句:

?
1
RedirectMatch 301 /([0-9]+)/([0-9]+)/([0-9]+)/(.*)$ http: //www.domain.com/$4

搞定了!

来源: wprecipes

15.拒绝非人为的发布评论

原理是禁止(机器人)非人为访问 wp-comments-post.php 文件, 可有效防止大量的垃圾评论。将第四行的 yourblog.com 改成您的网址即可。

?
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

16.将访问者重定向到一个维护模式页面

在升级博客或者修改主题的时候,让访客看到这些变化(特别是代码错误),总不是件好事,甚至可能引发安全问题,这时,就有必要将所有访问者引向一个指定的维护模式页面。下面的语法中使用302重定向,将读者引导到一个叫做“maintenance.html”的页面,第三行中是您自己博客的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]

来源: woueb

17.仅允许您自己的 IP 访问 wp-admin 目录

除非是多作者博客,如果您使用的是静态IP,可以设定只有您自己可以访问 wp-admin 目录,使用“allow from xx.xx.xxx.xx“ 这样的格式,多个静态IP各占一行。

?
1
2
3
4
5
6
7
8
9
AuthUserFile /dev/null
AuthGroupFile /dev/null
AuthName "Example Access Control"
AuthType Basic
<LIMIT GET>
order allow, deny
deny from all
allow from xx.xx.xx.xx
</LIMIT>

来源: reubenyau

18.将特定的 IP 列入黑名单

使用“deny from xxx.xx.xxx.xxx”这样的格式,不同的 IP 各占五行,这种方法可用于防止垃圾评论或某些您想禁止访问的访客。

?
1
2
3
4
5
<Limit GET POST>
order allow,deny
deny from 222.69.116.166
allow from all
</Limit>
目录
相关文章
|
7天前
|
Java 应用服务中间件 PHP
Nginx配置文件解释
Nginx配置文件解释
16 1
|
11月前
|
存储 安全 Shell
|
11月前
|
XML 数据格式
小工具:批量替换文件夹下所有文件内容中的指定词
函数作用:找出某文件夹下的包含指定关键词文件列表,并将关键字修改为目标字并将新内容保存至源文件
277 0
|
搜索推荐 机器人 定位技术
怎样写Robots文件?
在SEO优化网站结构时,控制网页抓取、索引是常用的技术。常用工具包括: 机器人文件。
132 0
怎样写Robots文件?
|
数据安全/隐私保护 网络安全 Windows