10 awesome .htaccess hacks for WordPress

10 awesome .htaccess hacks for WordPress

Posted by Jean-Baptiste Jung on Mar 19, 2009 | 203 comments

.htaccess, the file which control the Apache webserver, is very useful and allows you to do a lot of things. In this article, let’s see how .htaccess can help you with your WordPress blog, for both security,functionnality and usability.

Author : Jean-Baptiste Jung

Jean is a 27 years old self-taught web developer and WordPress specialist who lives in Wallonia, the French speaking part of Belgium. In addition to Cats Who Code, he also blogs about WordPress at WpRecipes and about Photoshop at PsdVibe.

Warning

When editing or modifying the .htaccess file of your WordPress blog, make sure to always have a backup that you can restore in case of something went wrong.

1 - Redirect WordPress RSS feeds to feedburner with .htaccess

Which blogger doesn’t use feedburner? Sure, feedburner is a very nice service, allowing you to know how many people suscribed to your rss feeds. The only problem is that you must edit your theme files to manually change the rss url. Happilly, there’s a nice hack, using .htaccess, which will make you save a lot of time!
Don't forget to modify line 6 before applying this code!

# temp redirect wordpress content feeds to feedburner
<IfModule mod_rewrite.c>
 RewriteEngine on
 RewriteCond %{HTTP_USER_AGENT} !FeedBurner    [NC]
 RewriteCond %{HTTP_USER_AGENT} !FeedValidator [NC]
 RewriteRule ^feed/?([_0-9a-z-]+)?/?$ http://feeds2.feedburner.com/catswhocode [R=302,NC,L]
</IfModule>

Source: How to redirect WordPress rss feeds to feedburner

2 - Remove /category/ from your WordPress url

By default, WordPress category permalinks are displayed that way:

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

As you can see, the category in the url is pretty useless. Here’s how to remove it:
First backup your .htaccess file. Then, open it and append the following line:

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

Once saved, your categories pages will be displayed like this:

http://www.catswhocode.com/blog/wordpress

Source: How to remove category from your WordPress url

3 - Using browser cache

A very good way to optimize your blog loading time is to force the use of the browser cache. This code will not improve your blog loading time directly, but it will save some work to the server by sending a 304 not modified status when the requested element haven't been modified.

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

Source: Comment accelerer le temps de chargement de votre blog

4 - Compress static data

Do you know that it is possible to send compressed data to the visitors, which will be decompressed by the client? This code will definitely save you (and your visitor) bandwidth and reduce your pages weight.

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

5 - Redirect Day and name permalinks to /%postname%/

The first thing to do is to login to your WordPress admin, go to Settings → Permalinks and select custom. Fill out the field with /%postname%/.
Your permalinks will now look like the ones on this blog:

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

Now we got to redirect all backlinks using the old permalinks structure to the new permalink structure. To do so, you’ll have to edit the .htaccess file, located in WordPress root directory.
Paste the following line in your .htaccess:

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

Allright! You just changed your permalinks structure without loosing any backlinks!
Source: Redirect day and name permalinks to postname

6 - How to: Deny comment posting to no referrer requests

Are you sick and tired about the daily amount of spam comments received? Sure, there’s akismet, but here’s a little .htaccess trick to prevent spammers posting on your blog. The fact is that most spammers uses bots comming from nowhere. This code will look for the referrer (the page from where the commentator come from) and will deny commenting if the commentator try to access the wp-comments-post.php file without directly comming from your blog.
Just change the line 4 and specify your blog url there.

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]

Source: How to deny comment posting to no referrer requests

7 - Redirect visitors to a maintenance page

When you're upgrading your blog, or making theme/design changes, it isn't a good idea to let people see your blog being tweaked, sometimes with design or code problems, or even worst, security gaps.
The solution is to design a nice "maintenance page" and temporarily redirect your visitors to that page until you finished the maintenance.
Replace maintenance.html (line 2) by the page you'd like to redirect your visitors, and the IP adress on line 3 by your own ip.
Note that a 302 redirection is used, to avoid search engines indexing the maintenance page instead of your real homepage!

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

Source: Comment faire une page d'accueil pour les internautes

8 - Protect your WordPress blog from hotlinking

Hotlinking is the use of an image from one site into a web page belonging to another site. Many bloggers are hotlinked, and have their bandwidth used on another websites. This very helpful code will protect your WordPress blog from hotlinking.

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]

Source: How to protect your WordPress blog from hotlinking

9 - Allow only your IP adress on the wp-admin directory

Excepted the case of a collaborative blog, only you should be allowed to visit the wp-admin directory. If you have a static IP, this code will do the job.
All you have to do is to enter your static IP adress on line 8. Note that you can add more IPs if needed, by creating a new line with: allow from xx.xx.xxx.xx inside.

AuthUserFile /dev/null
AuthGroupFile /dev/null
AuthName "Example Access Control"
AuthType Basic
<LIMIT GET>
order deny,allow
deny from all
allow from xx.xx.xx.xx
</LIMIT>

Source: Protecting the WordPress wp-admin folder

10 - Banning a WordPress Spammer With .htaccess

You know it, spam is very annoying. In the case of a particular person/bot spamming you, you can easily avoid it by blacklisting the IP.
Simply replace the IP adress on line 3 by the spammer's IP. You can add more spammers by creating a new line with deny from xxx.xx.xxx.xxx inside.

<Limit GET POST>
order allow,deny
deny from 200.49.176.139
allow from all
</Limit>

Source: The easiest way to ban a WordPress spammer

203 reactions

» Comments RSS Feed

Trackbacks

  1. wpressd.com
  2. 10 hacks .htaccess pour Wordpress | taggle.org
  3. Lorissa Shepstone – links for 2009-03-19
  4. You are now listed on FAQPAL
  5. 10 Awesome .htaccess Hacks for WordPress — Altitude Development Center
  6. 10个WordPress的.htaccess技巧(强烈推荐) at E-space
  7. Back to the fast Blog | Biggle's Blog
  8. lillbra » Blog Archive » links for 2009-03-19
  9. WordPress Tutorial How to Add Flickr Photo Widget to Sidebar | Socialseoguide.com
  10. links for 2009-03-20
  11. 考拉
  12. O metodă delicată de a scapa de spam » zoso's blog
  13. 10个WordPress的.htaccess技巧 | digAu☆掘金
  14. designtick.com
  15. Web design bookmarks
  16. BlogBuzz March 21, 2009
  17. HowTo use .htaccess with WordPress » WPDesignBlog - wordpress design blog , wordpress themes, themes, design, wp themes, webdesign, templates
  18. 10个WordPress的.htaccess技巧 - cOloR bOx
  19. Weekend Links - Mar 21, 2009 | OMNINOGGIN
  20. Top tutorials week ending 03/21/09 | FAQPAL Blog
  21. 世外桃源 » 10个WordPress的.htaccess技巧
  22. Weekend Roundup #40
  23. Frühjahrsputz | nodomain.cc
  24. Cum să scapi de spam « Blog-ul lui CrisM şi Maria
  25. WordPress Elite: 10 Awesome .htaccess Edits | boomtchik.com
  26. 10 trucos .htaccess muy útiles para WordPress | Un poco de mucho.com
  27. ::Digital Shippuden:: » 10 trucos de .htaccess para WordPress
  28. WordPress Elite: 10 Awesome .htaccess Edits | zooped.org
  29. 10 .htaccess Hacks To Speedup WordPress | One Tip A Day
  30. 3 More Plugins To Speedup WordPress | Connected Internet
  31. WordPress .htaccess 的10个Hack技巧 | WordPress China(非官方)
  32. 10 отличных приемов .htaccess для WP, блог дизайнера, WordPress, приемы Wordpress, все о Wordpress | Журнал Собиратель радостей
  33. WordPress 的10个 .htaccess 技巧 - 小人物大坏蛋
  34. Wordpress Blog Services - 10 Exceptional WordPress Hacks
  35. Wordpress Links « Andy Widodo - development blog
  36. Most Exceptional WordPress Hacks « Online Free Applications Software Tips Tools Wallpapers
  37. Wordpress-Hacks - Gestern war ich mal wieder auf Weltreise, Wartungsseite einrichten, Links zu ähnlichen Artikeln, Suchbegriffe bei den Suchergebnissen hervorheben, Wordpress-Themes - die Dateien und deren Bedeutungen, Der eigene kostenlose Blog Eine Wor
  38. 10 Exceptional WordPress Hacks « the gypsy
  39. 10 Cool WordPress Hacks | Knowledge Base eDynamo
  40. WordPress World: 50+ excellent WordPress Plugins, articles and resources - Stalkk.ed
  41. WordPress .htaccess Hacks | Web Design
  42. Viidar.net » Hjelp! WordPress-databasen min er hacket!
  43. Änderung der Wordpress Permalinkstrukur und Verzeichnis - IT-Pulse
  44. Hacks til WordPress « Tech.BusinessClass.dk
  45. 10 liens pour étendre Wordpress | Techno-blog.net
  46. WordPress: Plugins, Themes, Tipps & Tricks Collection. | Oberlehrer
  47. Tips to Speed up Wordpress | Stringer Magazine
  48. WordPress 的 10 个 .htaccess 技巧 | 芒果
  49. Die-for plugins » Anime²
  50. WP .htaccess « YZ WEBDESIGN
  51. GRINDSTONE MEDIA – 10 Wordpress .htaccess hacks awesomeness
  52. Wordpress的10个.htaccess技巧 | MarK Zhao
  53. 10 awesome .htaccess hacks for WordPress
  54. links for 2009-07-03 « williamlong’s blog
  55. links for 2009-07-03 |
  56. A to Z of WordPress .htaccess Hacks | WPShout.com
  57. 10 Exceptional WordPress Hacks « Web 2.0.
  58. ijse blog » 10个WordPress的.htaccess技巧(强烈推荐)
  59. 从A到Z!26个用于 Wordpress 的 .htaccess 规则 - 葡萄树 On The Road — 我只是一个会操作计算机的民工
  60. 从A到Z!26个用于 Wordpress 的 .htaccess 规则 | 所以说
  61. Waking Up To Problems : Thoughtless Banter
  62. 10个WordPress的.htaccess技巧(强烈推荐) « ijse blog
  63. 润物细无声 » WordPress .htaccess 的10个Hack技巧
  64. They keep attacking my WP sites...HELP!
  65. 30+ Useful WordPress Tutorials
  66. 10 New Wordpress Hacks - Themeflash : One Stop For All Your Web Resources
  67. Links and info for beginner blogger | The Old Gamer
  68. 10个神奇的.htaccess技巧(for WordPress) | 帕兰映像
  69. Top 10 ways to stop spam in WordPress
  70. Top 10 ways to stop spam in WordPress
  71. 卧雪眠云 » 10个WordPress的.htaccess技巧
  72. 15+ Tips & Resources for Learning and using .htaccess – An Intro for Web Designers | AndySowards.com :: Professional Web Design, Development, Programming Freelancer, Hacks, Downloads, Math and being a Web 2.0 Hipster?
  73. 15+ Tips & Resources for Learning and using .htaccess – An Intro for Web Designers | AndySowards.com :: Professional Web Design, Development, Programming Freelancer, Hacks, Downloads, Math and being a Web 2.0 Hipster?
  74. PHOQU » Blog Archive » Preventing Wordpress Porn via PHP
  75. WordPress News for 3/31/09 | Projektet II – tankar om ehandel, ledarskap och affärer
  76. Anointed Support » Blog Archive » Stop spam dead in it’s tracks
  77. 10 Exceptional WordPress Hacks | 9Tricks.Com - Tips - Tricks - Tutorials
  78. NamrouD | Upgrade Your Mind ! » 30 Incredibly Useful WordPress Hacks
  79. Top 10 ways to stop spam in WordPress « Doeasyway
  80. Wordpress Tricks And Hacks » Softloads
  81. 10 исключительных WordPress хаков | АртБухта
  82. Top 10 ways to stop spam in WordPress | meshdairy
  83. 搜集岛 » 十个WordPress的.htaccess技巧

Leave a comment