当前位置: 移动技术网 > 网络运营>服务器>Linux > Apache服务器中.htaccess文件的实用配置示例集锦

Apache服务器中.htaccess文件的实用配置示例集锦

2019年04月30日  | 移动技术网网络运营  | 我要评论

.htaccess 是什么?

htaccess (hypertext access,超文本访问) 是为网站所有者提供用于控制服务器环境变量以及其它参数的选项,从而增强他们网站的功能的文件。这些文件可以在网站目录树的任何一个目录中,并向该目录以及目录中的文件和子目录提供功能。

这些功能是什么呢?其实这些是服务器的指令,例如命令服务器执行特定任务的行,这些命令只对该文件所在目录中的文件和子目录有效。这些文件默认是隐藏的,因为所有操作系统和网站服务器默认配置为忽略它们,但如果查看隐藏文件的话,你就可以看到这些特殊文件。后续章节的话题将讨论能控制什么类型的参数。

注意:如果 .htaccess 文件保存在 /apache/home/www/gunjit/ 目录,那么它会向该目录中的所有文件和子目录提供命令,但如果该目录包含一个名为 /gunjit/images/ 子目录,且该子目录中也有一个 .htaccess 文件,那么这个子目录中的命令会覆盖父目录中 .htaccess 文件(或者目录层次结构中更上层的文件)提供的命令。

apache server 和 .htaccess 文件

apache http server 俗称为 apache,是为了表示对一个有卓越战争策略技能的美洲土著部落的尊敬而命名。它基于 ncsa httpd 服务器 ,是用 c/c++ 和 xml 建立的跨平台 web 服务器,它在万维网的成长和发展中起到了关键作用。

它最常用于 unix,但 apache 也能用于多种平台,包括 freebsd、linux、windows、mac os、novel netware 等。在 2009 年,apache 成为第一个为超过一亿站点提供服务的服务器。

apache 服务器可以让 www/ 目录中的每个用户有一个单独的 .htaccess 文件。尽管这些文件是隐藏的,但如果需要的话可以使它们可见。在 www/ 目录中可以有很多子目录,每个子目录通过用户名或所有者名称命名,包含了一个站点。除此之外你可以在每个子目录中有一个 .htaccess 文件,像之前所述用于配置子目录中的文件。

下面介绍如果配置 apache 服务器上的 htaccess 文件。

apache 服务器上的配置

这里有两种情况:

在自己的服务器上托管网站

在这种情况下,如果没有启用 .htaccess 文件,你可以通过在 http.conf(apache http 守护进程的默认配置文件) 中找到 部分启用。

<directory"/var/www/htdocs">

定位如下行

allowoverridenone

更改为

allowoverrideall

现在,重启 apache 后就启用了 .htaccess。

11个实用的apache .htaccess配置
1. 强制后缀反斜杠
在url的尾部加上反斜杠似乎对seo有利 :)

<ifmodule mod_rewrite.c> 
 rewritecond %{request_uri} /+[^\.]+$ 
 rewriterule ^(.+[^/])$ %{request_uri}/ [r=301,l] 
</ifmodule> 

2. 防盗链
节省你宝贵的带宽吧!

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] 

3. 重定向移动设备
加入你的网站支持移动设备访问的话,最好还是重定向移动设备的访问到专门定制的页面

rewriteengine on 
rewritecond %{request_uri} !^/m/.*$ 
rewritecond %{http_accept} "text/vnd.wap.wml|application/vnd.wap.xhtml+xml" [nc,or] 
rewritecond %{http_user_agent} "acs|alav|alca|amoi|audi|aste|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-" [nc,or] 
rewritecond %{http_user_agent} "dang|doco|eric|hipt|inno|ipaq|java|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-" [nc,or] 
rewritecond %{http_user_agent} "maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|opwv" [nc,or] 
rewritecond %{http_user_agent} "palm|pana|pant|pdxg|phil|play|pluc|port|prox|qtek|qwap|sage|sams|sany" [nc,or] 
rewritecond %{http_user_agent} "sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo" [nc,or] 
rewritecond %{http_user_agent} "teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|w3cs|wap-|wapa|wapi" [nc,or] 
rewritecond %{http_user_agent} "wapp|wapr|webc|winw|winw|xda|xda-" [nc,or] 
rewritecond %{http_user_agent} "up.browser|up.link|windowssce|iemobile|mini|mmp" [nc,or] 
rewritecond %{http_user_agent} "symbian|midp|wap|phone|pocket|mobile|pda|psp" [nc] 
#------------- the line below excludes the ipad 
rewritecond %{http_user_agent} !^.*ipad.*$ 
#------------- 
rewritecond %{http_user_agent} !macintosh [nc] #*see note below 
rewriterule ^(.*)$ /m/ [l,r=302] 

4. 强制浏览器下载指定的文件类型
你可以强制浏览器下载某些类型的文件,而不是读取并打开这些文件,例如mp3、xls。

<files *.xls> 
 forcetype application/octet-stream 
 header set content-disposition attachment 
</files> 
<files *.eps> 
 forcetype application/octet-stream 
 header set content-disposition attachment 
</files> 

5. 火狐的跨域名字体嵌入
火狐不允许嵌入一个外站的字体,下面的.htaccess片段可以绕过这个限制

<filesmatch "\.(ttf|otf|eot|woff)$"> 
<ifmodule mod_headers.c> 
  header set access-control-allow-origin "http://yourdomain.com" 
</ifmodule> 
</filesmatch> 

6. 使用.htaccess缓存 给网站提速
恐怕这个是最有用的代码片段了。这段代码能帮你极大的提高网站的速度!

# 1 year 
<filesmatch "\.(ico|pdf|flv)$"> 
header set cache-control "max-age=29030400, public" 
</filesmatch> 
# 1 week 
<filesmatch "\.(jpg|jpeg|png|gif|swf)$"> 
header set cache-control "max-age=604800, public" 
</filesmatch> 
# 2 days 
<filesmatch "\.(xml|txt|css|js)$"> 
header set cache-control "max-age=172800, proxy-revalidate" 
</filesmatch> 
# 1 min 
<filesmatch "\.(html|htm|php)$"> 
header set cache-control "max-age=60, private, proxy-revalidate" 
</filesmatch> 

7. 阻止wordpress博客的垃圾评论
还在为垃圾评论头疼吗?你可以用akismet插件来解决这个问题,但是.htaccess文件来的更直接:阻止垃圾评论机器人访问wp-comments-post.php文件

<ifmodule mod_rewrite.c> 
rewriteengine on 
rewritecond %{request_method} post 
rewritecond %{request_uri} .wp-comments-post\.php* 
rewritecond %{http_referer} !.*yourdomainname.* [or] 
rewritecond %{http_user_agent} ^$ 
rewriterule (.*) ^http://%{remote_addr}/$ [r=301,l] 
</ifmodule> 

8.重定向不同的feed格式到统一的格式
很多年前,有很多不同的feed格式,例如rss、atom、rdf等等。但是现在rss已经占了绝对的主导地位。下面这段代码可以让你重定向不同的feed格式到同一个feed。这段代码可以直接在wordpress博客上使用。

<ifmodule mod_alias.c> 
 redirectmatch 301 /feed/(atom|rdf|rss|rss2)/?$ http://example.com/feed/ 
 redirectmatch 301 /comments/feed/(atom|rdf|rss|rss2)/?$ http://example.com/comments/feed/ 
</ifmodule> 

9. 配置网站的html5视频
html5为我们带来了不用flash的视频播放功能,但是你必须配置你的服务器来提供最新的html5视频播放功能。

rewritecond %{request_filename} !-f 
rewritecond %{request_filename} !-d 
rewritecond %{request_uri} !=/favicon.ico 
addtype video/ogg .ogv 
addtype video/ogg .ogg 
addtype video/mp4 .mp4 
addtype video/webm .webm 
addtype application/x-shockwave-flash swf 

10. 记录php错误
在页面上显示php错误是很尴尬的事情,也不安全,下面这段代码可以把php错误记录到.log文件中而不在页面显示。

# 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 

11. 在javascript代码中运行php
在js中插入php代码有时候是很有用的,例如读取数据库。下面这段代码可以让你在js中运行php。

addtype application/x-httpd-php .js 
addhandler x-httpd-php5 .js 
 
<filesmatch "\.(js|php)$"> 
sethandler application/x-httpd-php 
</filesmatch> 

12. 404页面跳转,跳转到404.php,根据url记录不存在页面的路径

<ifmodule mod_rewrite.c> 
rewriteengine on 
 
rewritecond %{request_filename} !-f  
rewritecond %{request_filename} !-d  
rewritecond %{query_string} !^$ 
rewriterule ^/?(.*)$ /404.php?url=%{http_host}%{request_uri}?%{query_string} [nc]  
 
rewritecond %{request_filename} !-f  
rewritecond %{request_filename} !-d  
rewritecond %{query_string} ^$ 
rewriterule ^/?(.*)$ /404.php?url=%{http_host}%{request_uri} [nc]  
 
</ifmodule> 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网