.htaccess的应用实例

.htaccess是一个非常强大的分布式配置文件,学会使用.htaccess,对网站用户来说,可以实现众多的功能。这里我们可以罗列一下通过修改.htaccess文件来增强网站的功能的一些有用实例。

feed的重定向设置:
RewriteRule ^feed/?([_0-9a-z-]+)?/?$ http://feeds2.zl5.info/xxx [R=302,NC,L]

在wordpress的url中去除/category/
RewriteRule ^category/(.+)$ http://www.zl5.info/$1 [R=301,L]

使用浏览器缓存加速wordpress访问速度
FileETag MTime Size

ExpiresActive on
ExpiresDefault "access plus 1 year"

开启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


把带有日期格式的永久链接形式重定向到/%postname%/格式

RedirectMatch 301 /([0-9]+)/([0-9]+)/([0-9]+)/(.*)$ http://www.zl5.info/$4

阻止没有referrer requests的评论减少垃圾评论
RewriteEngine On
RewriteCond %{REQUEST_METHOD} POST
RewriteCond %{REQUEST_URI} .wp-comments-post.php*
RewriteCond %{HTTP_REFERER} !.*zl5.info.* [OR]
RewriteCond %{HTTP_USER_AGENT} ^$
RewriteRule (.*) ^http://%{REMOTE_ADDR}/$ [R=301,L]

重定向访客到维护页面

RewriteEngine on
RewriteCond %{REQUEST_URI} !/maintenance.html$
RewriteCond %{REMOTE_ADDR} !^123.123.123.123
RewriteRule $ /maintenance.html [R=302,L]

保护您的博客不被盗链
RewriteEngine On
#Replace ?xxx.com/ with your blog url
RewriteCond %{HTTP_REFERER} !^http://(.+.)?xxx.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]

只允许固定的ip地址访问wp-admin
AuthUserFile /dev/null
AuthGroupFile /dev/null
AuthName "Example Access Control"
AuthType Basic

order deny,allow
deny from all
allow from xxx.xxx.xxx.xxx

屏蔽固定ip访问
order allow,deny
deny from xxx.xxx.xxx.xxx
allow from all

posted @   ado-geek  阅读(208)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示