分类: 系统运维
2009-05-04 12:26:42
The .htaccess file allows you to easily improve your blog’s security, reduce bandwith and increase usability. In this post we’re going to look at 26 .htaccess hacks, from A to Z. If you enjoy this post then please grab the
Remember the golden rule:
Your can restrict access to wp-admin by IP:
1.
order deny,allow
2.
allow from a.b.c.d # This is your static IP
3.
deny from all
Source - BlogSecurity.net
One of the most important things you can do with .htaccess is blacklist IP addresses. You can do so with the following code:
1.
<
Limit
GET POST PUT>
2.
order allow,deny
3.
allow from all
4.
deny from 123.456.789
5.
LIMIT
>
Source -
Your wp-config file contains your database name, your database username and your database password. In other words, you’ll want to keep it secure.
1.
# protect wpconfig.php
code
>
2.
<
files
wp-config.php>
3.
order allow,deny
4.
deny from all
5.
files
>
Source - Josiah Cole
1.
# disable directory browsing
em
>
2.
<
em
>Options All -Indexes
Source- Josiah Cole
I bet if I asked you to explain exactly what .htaccess is, you’d struggle to tell me exactly. To be honest, until I wrote this, I wasn’t totally sure. Wikipedia explains in a nice, jargon free way:
.htaccess ( access) is the default name of -level that allow for decentralized management of configuration when placed inside the web tree.
The Wikipedia article then goes on, with some examples of common usage:
- ,
- .htaccess files are often used to specify the security restrictions for the particular directory, hence the filename “access.” The .htaccess file is often accompanied by a file which stores valid and their .
- Changing the page that is shown when a server-side error occurs, for example
- Servers often use .htaccess to rewrite long, overly comprehensive URLs to shorter and more memorable ones.
- .htaccess files allow a server to control used by to reduce usage, load, and perceived .
Feedburner is a blogger’s best friend. Trouble is, directing your feed to it is a bit of a pain. The solution: a .htaccess hack of course!
1.
# temp redirect WordPress content feeds to feedburner
2.
<
IfModule
mod_rewrite.c>
3.
RewriteEngine on
4.
RewriteCond %{HTTP_USER_AGENT} !FeedBurner [NC]
5.
RewriteCond %{HTTP_USER_AGENT} !FeedValidator [NC]
6.
RewriteRule ^feed/?([_0-9a-z-]+)?/?$ [R=302,NC,L]
7.
IfModule
>
Source -
This is quite complicated, so check out the source below. In a nutshell it is a way of getting round using Javascript (because it doesn’t do the SEO any good).
Source - adityaspeaks.com
Hotlinking. According to , also known as “leeching, piggy-backing, direct linking, offsite image grabs and bandwidth theft”. In other words it is using an image from another site. If people do it to you, it’ll use up your bandwith. You can stop it with the .htaccess hack below.
1.
#disable hotlinking of images with forbidden or custom image option
2.
RewriteEngine on
3.
RewriteCond %{HTTP_REFERER} !^$
4.
RewriteCond %{HTTP_REFERER} !^(www\.)?yourdomain.com/.*$ [NC]
5.
#RewriteRule \.(gif|jpg)$ - [F]
6.
RewriteRule \.(gif|jpg)$ [R,L]
Source - Josiah Cole
Yeah, ok, I got a bit desperate trying to find something that begins with ‘I’ :P. But, that doesn’t mean this isn’t useful; it’s very important!
Backup. Always, always make sure you have a backup to hand; the slightest mistake will be fatal.
If something goes wrong it is always helpful for visitors to have an email they can contact. You can display
It isn’t nice when people steal your content. One of the ways ‘content thieves’ scrape content from sites is by simply using your RSS feed. If you’ve got the scraper’s IP address (which is very easy to do; Google it) then you can use your .htaccess file to block the scraper. The code below redirects a site taking your feed back to another feed (ie their feed). Replace the IP on line two with the offending site’s and the feed on line three with the offending site’s feed.
1.
RewriteEngine on
2.
RewriteCond %{REMOTE_ADDR} ^69.16.226.12
3.
RewriteRule ^(.*)$
Source -
To limit the number of simultaneous connections to a directory or your entire site, use the below line. If you place it in a directory other than the root directory, then it will limit the connections to that directory and its sub-directories only. Placing it in htaccess file of root directory will implement it for entire site.
1.
MaxClients <
number-of-connections
>
Source - Pix.l|ne
You block spammers, everyone or just yourself using the code below. See ‘N’ for another spam-stopping technique.
1.
Order allow,deny
2.
Deny from <
incoming
-address >
3.
Allow from <
incoming
-address>
Source - pix.l|ne
It doesn’t matter what the reason is, at some point in your life you’ll probably want to make maintenance page. Replace “/maintenance.html” with whatever the url of your maintenance page is and put your own IP address on line three.
1.
RewriteEngine on
2.
RewriteCond %{REQUEST_URI} !/maintenance.html$
3.
RewriteCond %{REMOTE_ADDR} !^123\.123\.123\.123
4.
RewriteRule $ /maintenance.html [R=302,L]
Source - CatsWhoCode/Woueb.net
Slightly simpler than the spam-stopping solution under ‘S’, what this hack does is utilise the fact that most spammes use bots coming from ‘nowhere’. The hack checks to see where a comment is coming from, and if it is coming from ‘nowhere’ then it blocks it. Simple.
1.
RewriteEngine On
2.
RewriteCond %{REQUEST_METHOD} POST
3.
RewriteCond %{REQUEST_URI} .wp-comments-post\.php*
4.
RewriteCond %{HTTP_REFERER} !.*yourblog.com.* [OR]
5.
RewriteCond %{HTTP_USER_AGENT} ^$
6.
RewriteRule (.*) ^{REMOTE_ADDR}/$ [R=301,L]
Source -
If you’re offering files for download then the hack below will be very useful - it forces files to save as instead of opening or streaming.
1.
AddType
span
> application/octet-stream .avi .mpg .mov .pdf .xls .mp4
Source -
After you’ve spent all that time protecting your blog from .htaccess attack, the last thing you want to do is leave your .htaccess file itself open to attack!The hack below prevents external access to any file with .hta (or any case insensitive variation). Place the code below in your domain’s root .htaccess file.
1.
# STRONG HTACCESS PROTECTION
code
>
2.
<
Files
~ "^.*\.([Hh][Tt][Aa])">
3.
order allow,deny
4.
deny from all
5.
satisfy all
6.
Files
>
Source: Perishable Press
If you’re paying for what bandwith you use, this article can save you cash!
Source -
1.
RedirectMatch 301 ^/blog/.*$
Source -
.htaccess is great for stopping comment spam, and Jeff over at Perishable Press has put together a huge blacklist you can copy and paste that should stop you getting so much spam! .
The hack below lets you set the timezone of the server:
1.
SetEnv
span
> TZ America/Indianapolis
Source -
Having /category/ in a category URL seems a bit useless. How do I get rid of it, I hear you cry! A .htaccess hack, of course!
1.
RedirectMatch 301 ^/category/(.+)$ $1
2.
# OR
3.
RewriteRule ^category/(.+)$ $1 [R=301,L]
Source:
Yep. I got desperate. Well what .htaccess trick can you think of that starts with ‘v’?
This neat trick will auto-correct simple URL spelling mistakes
1.
<
IfModule
mod_speling.c>
2.
CheckSpelling On
3.
IfModule
>
Source -
Using a 301 (permanent) redirect, you can move all visitors to to
1.
# permanently redirect from www domain to non-www domain
2.
RewriteEngine on
3.
Options +FollowSymLinks
4.
RewriteCond %{HTTP_HOST} ^www\.domain\.tld$ [NC]
5.
RewriteRule ^(.*)$ $1 [R=301,L]
Source:
Xenophobic: “an intense fear or dislike of forigners or strangers”
I think it is quite appropriate to call your wp-login page xenophobic if you install this hack; it won’t let anyone access it apart from yourself!
1.
<
Files
wp-login.php>
2.
Order deny,allow
3.
Deny from All
4.
Allow from 123.456.789.0
5.
Files
>
Source -
What do you do if your server doesn’t like the .htaccess file format? Rename the .htaccess file! You can rename it to whatever you like, using the code below:
1.
# rename htaccess files
code
>
2.
<
code
>AccessFileName ht.access
Source -
So you want to be able to put the word ‘zygote’ in your .htaccess file? You’ll be needing to make a comment. Comments are really easy to do, just use # at the beginning of a line, which tells the server to ignore the line.
1.
# see - this is a comment - you can only use letters and numbers and - and _ That is why there are no commas