Twist Training

MoreNiche Affiliate SEO Tutorials

Advanced Control of.Htaccess

The basics you have already seen are just a fraction of the things you can control with .htaccess. There are many, many more possibilities but remember that some shared hosts block or disable certain .htaccess functions. Remember to check with your administrator if you problems.

Move Your Whole Site

##Redirect whole site##
Redirect 301 / http://www.example.com

Does exactly what it says on the tin, everything will go to the new url.

Redirecting Subdirectories

##Redirect a subdirectory to temporary page##
RedirectMatch 302 ^/articles(.*)$ http://www.example.com/temporary-page.php

##Redirect a subdirectory to a new one##
RedirectMatch 301 ^/articles(.*)$ http://www.example.com/new-articles/

These codes can be used for dealing with subdirectories that are going through change. They will redirect ALL requests for things below the subdirectory and send them to the new page or directory.

The first one is used for times when you may be upgrading a part of your site or performing maintenance, it will temporarily send people to a different page which you can design and place an apology on and give an estimated time of return. Don’t forget to remove it when you’re done!

The second one is to be used when you have retired an old subdirectory that may contain an older CMS or set of information that many people have linked to or bookmarked and sending it to a new directory.

Change File Type Requests

##Code to make all HTML requests become PHP requests##
RedirectMatch 301 (.*)\.html$ http://www.example.com$1.php

This code can be very handy but should be used with great care. If you upgrade to a different CMS or file structure you may find that many of your old pages have links or traffic and making separate redirects for them all in simply too much.

This code will redirect ALL, and I repeat that, ALL requests for a html file into a PHP file. It can work both ways around if you simply swap PHP and html but this should only be used if you are sure no html files are need, this means includes and scripts too.

Checkspelling

##Make Linux correct case sensitive urls##
CheckSpelling On

This is a fun one; you may have notice that some Linux servers generate 404 because of placing capitals in URLs. This feature will not only correct that but also correct tiny spelling error people make in urls they type in.

There is a downside though as some programs can get a little confused by the way checkspelling handles things, many shared hosts also do not allow access to this kind of modification. Only use this one if you are sure you can.

Edit Default Index Page

##Change default index##
DirectoryIndex myindex.html

If for some reason you need to stop people using index this will make them go to whatever you tell it to instead.

URL Rewriting

PHP variables can usually be indexed by search engines these days but it is still nice to display user friendly links. Here is a rewrite example based on the page being http://www.example.com/news.php?story=101

RewriteRule news/story/(.*)/ news.php?story=$1
RewriteRule news/story/(.*)
news.php?story=$1

This will output the URL as:

http://www.example.com/news/stroy/101

URL rewrite is very powerful and can be very, very complex, if you are a more hands off person this tool can be a great help if you want to learn more then visit this page.

Non SEO but Still useful Commands

You .htaccess file is useful for a whole bunch of little things, this stuff isn’t really SEO related but can be a massive help to know how to do. Just keep in mind that shared hosting may prevent you from using some of these features, if they don’t work contact your system administrator.

IP Control

##IP Control##
order allow,deny
deny from 111.22.3.4
deny from 333.22.11.
allow from all

This code will allow you to block certain IPs (and users) or blocks depending on what you enter. It can be useful if you are being persistently attacked or spammed.

Blocking Bots

Some people send robots around to spam the crap out of your site, scrape content and worse, pasting the following code into your .htaccess will reduce the number of bots that can access your site. The list is courtesy of JavascriptKit and I use it on many of my sites with no problems.

##Block Nastly Bots##
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} ^BlackWidow [OR]
RewriteCond %{HTTP_USER_AGENT} ^Bot\ mailto:craftbot@yahoo.com [OR]
RewriteCond %{HTTP_USER_AGENT} ^ChinaClaw [OR]
RewriteCond %{HTTP_USER_AGENT} ^Custo [OR]
RewriteCond %{HTTP_USER_AGENT} ^DISCo [OR]
RewriteCond %{HTTP_USER_AGENT} ^Download\ Demon [OR]
RewriteCond %{HTTP_USER_AGENT} ^eCatch [OR]
RewriteCond %{HTTP_USER_AGENT} ^EirGrabber [OR]
RewriteCond %{HTTP_USER_AGENT} ^EmailSiphon [OR]
RewriteCond %{HTTP_USER_AGENT} ^EmailWolf [OR]
RewriteCond %{HTTP_USER_AGENT} ^Express\ WebPictures [OR]
RewriteCond %{HTTP_USER_AGENT} ^ExtractorPro [OR]
RewriteCond %{HTTP_USER_AGENT} ^EyeNetIE [OR]
RewriteCond %{HTTP_USER_AGENT} ^FlashGet [OR]
RewriteCond %{HTTP_USER_AGENT} ^GetRight [OR]
RewriteCond %{HTTP_USER_AGENT} ^GetWeb! [OR]
RewriteCond %{HTTP_USER_AGENT} ^Go!Zilla [OR]
RewriteCond %{HTTP_USER_AGENT} ^Go-Ahead-Got-It [OR]
RewriteCond %{HTTP_USER_AGENT} ^GrabNet [OR]
RewriteCond %{HTTP_USER_AGENT} ^Grafula [OR]
RewriteCond %{HTTP_USER_AGENT} ^HMView [OR]
RewriteCond %{HTTP_USER_AGENT} HTTrack [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^Image\ Stripper [OR]
RewriteCond %{HTTP_USER_AGENT} ^Image\ Sucker [OR]
RewriteCond %{HTTP_USER_AGENT} Indy\ Library [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^InterGET [OR]
RewriteCond %{HTTP_USER_AGENT} ^Internet\ Ninja [OR]
RewriteCond %{HTTP_USER_AGENT} ^JetCar [OR]
RewriteCond %{HTTP_USER_AGENT} ^JOC\ Web\ Spider [OR]
RewriteCond %{HTTP_USER_AGENT} ^larbin [OR]
RewriteCond %{HTTP_USER_AGENT} ^LeechFTP [OR]
RewriteCond %{HTTP_USER_AGENT} ^Mass\ Downloader [OR]
RewriteCond %{HTTP_USER_AGENT} ^MIDown\ tool [OR]
RewriteCond %{HTTP_USER_AGENT} ^Mister\ PiX [OR]
RewriteCond %{HTTP_USER_AGENT} ^Navroad [OR]
RewriteCond %{HTTP_USER_AGENT} ^NearSite [OR]
RewriteCond %{HTTP_USER_AGENT} ^NetAnts [OR]
RewriteCond %{HTTP_USER_AGENT} ^NetSpider [OR]
RewriteCond %{HTTP_USER_AGENT} ^Net\ Vampire [OR]
RewriteCond %{HTTP_USER_AGENT} ^NetZIP [OR]
RewriteCond %{HTTP_USER_AGENT} ^Octopus [OR]
RewriteCond %{HTTP_USER_AGENT} ^Offline\ Explorer [OR]
RewriteCond %{HTTP_USER_AGENT} ^Offline\ Navigator [OR]
RewriteCond %{HTTP_USER_AGENT} ^PageGrabber [OR]
RewriteCond %{HTTP_USER_AGENT} ^Papa\ Foto [OR]
RewriteCond %{HTTP_USER_AGENT} ^pavuk [OR]
RewriteCond %{HTTP_USER_AGENT} ^pcBrowser [OR]
RewriteCond %{HTTP_USER_AGENT} ^RealDownload [OR]
RewriteCond %{HTTP_USER_AGENT} ^ReGet [OR]
RewriteCond %{HTTP_USER_AGENT} ^SiteSnagger [OR]
RewriteCond %{HTTP_USER_AGENT} ^SmartDownload [OR]
RewriteCond %{HTTP_USER_AGENT} ^SuperBot [OR]
RewriteCond %{HTTP_USER_AGENT} ^SuperHTTP [OR]
RewriteCond %{HTTP_USER_AGENT} ^Surfbot [OR]
RewriteCond %{HTTP_USER_AGENT} ^tAkeOut [OR]
RewriteCond %{HTTP_USER_AGENT} ^Teleport\ Pro [OR]
RewriteCond %{HTTP_USER_AGENT} ^VoidEYE [OR]
RewriteCond %{HTTP_USER_AGENT} ^Web\ Image\ Collector [OR]
RewriteCond %{HTTP_USER_AGENT} ^Web\ Sucker [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebAuto [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebCopier [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebFetch [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebGo\ IS [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebLeacher [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebReaper [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebSauger [OR]
RewriteCond %{HTTP_USER_AGENT} ^Website\ eXtractor [OR]
RewriteCond %{HTTP_USER_AGENT} ^Website\ Quester [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebStripper [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebWhacker [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebZIP [OR]
RewriteCond %{HTTP_USER_AGENT} ^Wget [OR]
RewriteCond %{HTTP_USER_AGENT} ^Widow [OR]
RewriteCond %{HTTP_USER_AGENT} ^WWWOFFLE [OR]
RewriteCond %{HTTP_USER_AGENT} ^Xaldon\ WebSpider [OR]
RewriteCond %{HTTP_USER_AGENT} ^Zeus
RewriteRule ^.* - [F,L]

Hot Linking

To be honest I am not one to care about hot linking but some people are. Hotlinking is when people embed images and media from your site into theirs which ends up putting load on your server and to be honest, is just rude.

##Kill Hotlinks##
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example.com/.*$ [NC]
RewriteRule \.(gif|jpg)$ http://www.example.com/donthotlink.php [R,L]

Make sure you change the domains and design a page or image to redirect people too. You can include additional file types by separating them by a pipe bar “|” and typing the extension inside the brackets on line 5.

Directory Listing

Sometimes people use directory listing to find things out about your site in order to hack it. Bad news. Most of the time servers stop this happening themselves but if you need to take matters into your own hands this little snippet of code will stop all directory listing.

IndexIgnore *

Protecting Files

You can add a measure of protection to files with .htaccess by using the following code. Most likely it will be your .htaccess you want to protect (if it has passwords in) but this will work for any file type. Obviously blocking images will cause a problem on your site so be careful.

##Block .htaccess Viewing##
<Files .htaccess>
order allow,deny
deny from all
</Files>

If you are using Wordpress many people protect their wp-config.php file with this command