I am pretty sure, most of the WordPress self hosted users are aware of what is .htaccess. I am not going to talk about what is it about, rather about its essential techiques that can be tried by every single WordPress users. There are a lot of .htaccess hacks, but here are some of it, which I find it essential.
Note: Before trying any of these hacks, kindly take a backup of .htaccess file from the WordPress root folder.
1. Remove the /category/ from WordPress category permalink
Personally I am not a big fan of /category/. If you look at some popular blogs, you can see them having categories http://yourdomain.com/wordpress instead of http://yourdomain.com/category/wordpress
RewriteRule ^category/(.+)$ http://www.yourblogname.com/$1 [R=301,L]
2. Redirect post with date and name to post with postname
Some of like to display post with date, month, year and postname. For those who wish to redirect all the post under this permalink to /%postname%/ this; here is the hack.
RedirectMatch 301 /([0-9]+)/([0-9]+)/([0-9]+)/(.*)$ http://www.yourblogdomain.com/$4
3. Stop images used in post from hotlinking
This happens to most of the blogs, who write tutorials where the other blogger shares your post on his blog by hotlinking your images. Going through this way, all the image shared in his blogs, takes your server bandwidth to load those images. The .htaccess hack used here will stop hotlinking of images and display a default image for hotlinked images. So create a new image with the name of stophotlink.jpg and upload it to your image folder.
RewriteEngine On #Replace ?mysite\.com/ with your blog url RewriteCond %{HTTP_REFERER} !^http://(.+\.)?yourblog\.com/ [NC] RewriteCond %{HTTP_REFERER} !^$ #Replace /images/nohotlink.jpg with your "don't hotlink" image url RewriteRule .*\.(jpe?g|gif|bmp|png)$ /images/stophotlink.jpg [L]
4. Restricting IP’s of comment spammers
Askimet is a beautiful tool to keep us free from spam comments. If you are not using Askimet, but want to just block certain IP’s of spammers, here is the tip
<Limit GET POST> order deny,allow deny from 12.23.34.56 deny from 45.67.78.90 allow from all </Limit>
5. Redirect visitors to maintenance page
We often change our WordPress theme. When changing the themes, normally the visitors encounter with strange errors. At this situation, we have to redirect the visitors to a maintenance web page except for the admin.
In the snippet below, change your maintenance page name and on REMOTE_ADDR type in your IP. Make sure that you have a static IP before trying this hack.
RewriteEngine on RewriteCond %{REQUEST_URI} !/maintenance.html$ RewriteCond %{REMOTE_ADDR} !^123\.123\.123\.123 RewriteRule $ /maintenance.html [R=302,L]
6. Redirect www to non www or vice versa
Normally we like to keep our URL short. This has also become a trend to keep your site name shorter as much as possible. So removing the www can considerably makes a positive effect for blogs with longer domain name.
Redirect www to non www
RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} ^www.yourblogname.com [NC] RewriteRule ^(.*)$ http://yourblogname.com/$1 [L,R=301]
Redirect non www to www
RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} ^yourblogname.com [NC] RewriteRule ^(.*)$ http://www.yourblogname.com/$1 [L,R=301]
7. Disable directory listing
This keep your blog visitors from looking at your directory
# disable directory browsing Options All -Indexes
8. Limit upload size from WordPress media option
# limit file uploads to 10mb LimitRequestBody 10240000
9. Redirect WordPress RSS to Feedburner
Feedburner is loved my many. But by default WordPress have its own RSS which doesn’t need any extra configuration. In order to redirect the default WordPress RSS to Feedburner, here is the hack.
# 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://feeds.feedburner.com/YOURFEEDNAME [R=302,NC,L] </IfModule>
10. Automatically fix spelling mistakes in URL
Nifty tweak for all WordPress users
<IfModule mod_speling.c> CheckSpelling On </IfModule>
11. Restrict access to WordPress admin by IP
Accessing WordPress dashboard based on IP can tighten the security from unauthorized entry.
order deny,allow allow from 93.127.124.34 # This is your static IP deny from all