Loading... <p><strong>.htaccess是什么?</strong></p> <p>.htaccess是使用UNIX或Linux搭建的服务器中的一个特殊的文件,这个文件只存在于Linux系统中,Win系列的主机是没有的。那 么.htaccess有什么功能呢?通俗点的讲,就是可以通过编写这个文件中的某些内容,进而实现.htaccess文件所在目录及其子目录的权限与功能 的设置,是自己的站点灵活多变,下面就介绍.htaccess文件关于WordPress的十个应用技巧,举一反三,这些应用技巧同样适用于其它站点程序。</p> <p>1. 重定向WordPress的RSS Feed链接地址到Feedburner地址: <br />除了修改WP的模板文件来定制其输出的RSS Feed链接地址外,还可以使用.htaccess文件来进行设置(替换yourrssfeedlink为自己的Feedburner地址)。</p> <p><font color="#4f81bd">/* <br /><IfModule mod_rewrite.c> <br />RewriteEngine on <br />RewriteCond %{HTTP_USER_AGENT} !FeedBurner [NC] <br />RewriteCond %{HTTP_USER_AGENT} !FeedValidator [NC] <br />RewriteRule ^feed/?([_0-9a-z-]+)?/?$ </font><span class="external-link"><a class="no-external-link" href="http://feeds2.feedburner.com/catswhocode" target="_blank"><i data-feather="external-link"></i><font color="#4f81bd">http://feeds2.feedburner.com/catswhocode</font></a></span><font color="#4f81bd"> [R=302,NC,L] <br /></IfModule> <br />*/</font></p> <p>大家使用时别忘了把代码中的Feedburner地址替换为自己的 </p> <p> <br />2. 使用浏览器缓存: <br />可以修改.htaccess文件让访问者使用浏览器缓存来优化其访问速度。</p> <p><font color="#4f81bd">/*FileETag MTime Size <br /><ifmodule mod_expires.c> <br /><filesmatch “\.(jpg|gif|png|css|js)$”> <br />ExpiresActive on <br />ExpiresDefault “access plus 1 year” <br /></filesmatch> <br /></ifmodule>*/</font></p> <p>3. 去除WordPress分类链接中的”/category/”: <br />默认情况下,WordPress的分类链接显示的样式为:</p> <p><span class="external-link"><a class="no-external-link" href="http://e-spacy.com/demoblog/category/tech" target="_blank"><i data-feather="external-link"></i>http://e-spacy.com/demoblog/category/tech</a></span></p> <p>其实其中的category部分没有任何意义,如果想去掉它可以修改.htaccess文件(替换yourblog为自己的网址)。</p> <p>RewriteRule ^category/(.+)$ <span class="external-link"><a class="no-external-link" href="http://www.yourblog.com/" target="_blank"><i data-feather="external-link"></i>http://www.yourblog.com/</a></span>$1 [R=301,L]</p> <p> <br />4. 阻止没有referrer来源链接的垃圾评论: <br />设置.htaccess文件可以阻止大多数无Refferrer来源的垃圾评论机器人Bot Spammer。其会查询访问你网站的来源链接,然后阻止其通过wp-comments-post.php来进行垃圾评论。</p> <p><font color="#4f81bd">RewriteEngine On <br />RewriteCond %{REQUEST_METHOD} POST <br />RewriteCond %{REQUEST_URI} .wp-comments-post\.php* <br />RewriteCond %{HTTP_REFERER} !.*yourblog.com.* [OR] <br />RewriteCond %{HTTP_USER_AGENT} ^$ <br />RewriteRule (.*) ^</font><span class="external-link"><a class="no-external-link" href="http://%{REMOTE_ADDR}/" target="_blank"><i data-feather="external-link"></i><font color="#4f81bd">http://%{REMOTE_ADDR}/</font></a></span><font color="#4f81bd">$ [R=301,L]</font></p> <p> <br />5. 重定向日期格式的WP Permalink链接地址为Postname格式: <br />如果你目前的Permalink地址为/%year%/%monthnum%/%day%/%postname%/ 的格式,那么我强烈推荐你直接使用/%postname%/ ,这样对搜索引擎要舒服得多。首先你需要在WordPress的后台设置输出的Permalinks格式为/%postname%/ 。然后修改.htaccess文件来重定向旧的链接,不然别人以前收藏你的网址都会转成404哦!(替换yourdomain为自己的网址)</p> <p>RedirectMatch 301 /([0-9]+)/([0-9]+)/([0-9]+)/(.*)$ <span class="external-link"><a class="no-external-link" href="http://www.yourdomain.com/" target="_blank"><i data-feather="external-link"></i>http://www.yourdomain.com/</a></span>$4</p> <p> <br />6. 压缩静态数据: <br />可以修改.htaccess文件来压缩需要访问的数据(传输后在访问端解压),从而可以减少访问流量和载入时间。</p> <p><font color="#4f81bd">AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/javascript text/css application/x-javascript <br />BrowserMatch ^Mozilla/4 gzip-only-text/html <br />BrowserMatch ^Mozilla/4.0[678] no-gzip <br />BrowserMatch bMSIE !no-gzip !gzip-only-text/html</font></p> <p>7. 阻止指定IP的访问: <br />如果你想要阻止指定IP的访问,来防止其垃圾评论,那么你可以创建自己的Backlist黑名单。(替换xx.xx.xx.xx为指定的IP地址)</p> <p><font color="#4f81bd"><Limit GET POST> <br />order allow,deny <br />deny from xx.xx.xx.xx <br />allow from all <br /></Limit></font></p> <p> <br />8. 只允许自己的IP访问wp-admin: <br />如果你不是团队合作Blog,最好设置只有自己能够访问WP的后台。前提是你的IP不是像我一样动态的哦。(替换xx.xx.xx.xx为自己的IP地址)</p> <p><font color="#4f81bd">AuthUserFile /dev/null <br />AuthGroupFile /dev/null <br />AuthName “Example Access Control” <br />AuthType Basic <br /><LIMIT GET> <br />order deny,allow <br />deny from all <br />allow from xx.xx.xx.xx <br /></LIMIT></font></p> <p> <br />9. 设置你的WordPress防盗链: <br />盗链是指其它网站直接使用你自己网站内的资源,从而浪费网站的流量和带宽,比如图片,上传的音乐,电影等文件。(替换mysite为自己的网址和/images/notlink.jpg为自己定制的防盗链声明图片)</p> <p><font color="#4f81bd">RewriteEngine On <br />#Replace ?mysite\.com/ with your blog url <br />RewriteCond %{HTTP_REFERER} !^</font><span class="external-link"><a class="no-external-link" href="http://(.+\.)?mysite\.com/" target="_blank"><i data-feather="external-link"></i><font color="#4f81bd">http://(.+\.)?mysite\.com/</font></a></span><font color="#4f81bd"> [NC] <br />RewriteCond %{HTTP_REFERER} !^$ <br />#Replace /images/nohotlink.jpg with your “don’t hotlink” image url <br />RewriteRule .*\.(jpe?g|gif|bmp|png)$ /images/nohotlink.jpg [L]</font></p> <p> <br />10. 定制访问者跳转到维护页面: <br />当你进行网站升级,模板修改调试等操作时,最好让访问者临时跳转到一个声明的维护页面(和404错误页面不同),来通知网站暂时无法访问,而不是留下一片 空白或者什么http bad错误。(替换maintenance.html为自己定制的维护页面网址,替换123.123.123.123为自己目前的IP地址,不然你自己访 问也跳转哦)</p> <p><font color="#4f81bd">RewriteEngine on <br />RewriteCond %{REQUEST_URI} !/maintenance.html$ <br />RewriteCond %{REMOTE_ADDR} !^123\.123\.123\.123 <br />RewriteRule $ /maintenance.html [R=302,L]/</font></p> <p> <br />英文原文: 10 awesome .htaccess hacks for WordPress</p> <p>地址:<span class="external-link"><a class="no-external-link" href="http://www.ifunkey.com/1779.html作者:FunKey" target="_blank"><i data-feather="external-link"></i>http://www.ifunkey.com/1779.html作者:FunKey</a></span></p> Last modification:October 6, 2023 © Allow specification reprint Support Appreciate the author AliPayWeChat Like 如果觉得我的文章对你有用,请随意赞赏
One comment
元宵节博主还忙啊..文学小说网(www.wenx.net)祝博主元宵节快乐了.永远支持博主!