Next Previous Contents

9. Create or modify your .htaccess file(s)

We've got just one more major step to perform, but it's really quite simple. If you're already familiar with .htaccess files, you will already understand the principle. If you don't know what an .htaccess file is, that's OK too because all you have to know is how to cut and paste using a text editor.

If you already have .htaccess files on your web site, download the main one from your home page where your main index.html or index.php (or whatever the starting point of your web site is) is located. Load this .htaccess file into any text editor. If you are currently not using any .htaccess files, then just use a text editor to create one. You will be cutting and pasting the following text into this file:

#---------------- start of country blocking -------------------

# This first segment is used when the current directory is requested but with
# just a trailing slash and no index.html name included in the URL.

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/country1/countrycheck.php
RewriteRule ^$ /country1/countrycheck.php?cc_indexfile=index.html [L]

# This second segment is used when any subdirectory is requested but with just
# a trailing slash and no index.html name included in the URL.

RewriteCond %{REQUEST_URI} !^/country1/countrycheck.php
RewriteRule ^(.*)/$ /country1/countrycheck.php?cc_indexfile=index.html [L]

# This third segment is used when an actual file is requested with the full
# path to the file included in the URL.

RewriteCond %{REQUEST_URI} ^.*\.htm [OR]
RewriteCond %{REQUEST_URI} ^.*\.html [OR]
RewriteCond %{REQUEST_URI} ^.*\.php
RewriteCond %{REQUEST_URI} !^/country1/countrycheck.php
RewriteRule ^(.+) /country1/countrycheck.php [L]

#---------------- end of country blocking -------------------

If you're creating a new .htaccess file, this is all you need. If you're modifying an existing one, then paste it somewhere either near the start of the file or near the end of the file, depending on what ever else is in the file. You might have some other tests being performed that need to run first, or maybe not. If you're not familiar with what you see in the file, then paste in the above at the start of the file. If you really know what you are doing, then you will be able to figure out where to paste it.

Now that we have the above text pasted in, we need to make some minor edits. You'll notice that "/country1/countrycheck.php" is listed 6 times. Change the "country1" part to whatever you called the new directory you created. If you created the new directory inside the main "public_html" (called "httpdocs" on some systems and maybe something else on other systems) or wherever it is that your web site is located, then that's all you have to do. If however, you created it one or more subdirectory levels down, then be sure to adjust it accordingly. For example, if you created a new "country1" directory in the "newstuff" directory, you will edit the above lines to look like:

/newstuff/country1/countrycheck.php

Make the changes to all 6 references in your .htaccess file. Now, one more thing to edit. In your home page, there is a default index file called something like "index.html" or "index.php" or maybe something else. But whatever it is called, you need to include this in the .htaccess file. The countrycheck.php program needs to know what file to send when someone accesses your website like this:

http://yourwebsite.com/

The countrycheck.php program will send the index.html file to the user. If your default index file is named differently, edit it in the first segment where you see the following line:

RewriteRule ^$ /country1/countrycheck.php?cc_indexfile=index.html [L]

Where it says "cc_indexfile=index.html", change the "index.html" to whatever your default index file is named.

The next segment is similar, but this one is used for any subdirectory requests below the one where this .htaccess file will reside. For instance, when the following request is received:

http://www.yourwebsite.com/games/morestuff/

There is an index file in that subdirectory as well. Edit the following line that you pasted in your .htaccess file. This one is in the second segment:

RewriteRule ^(.*)/$ /country1/countrycheck.php?cc_indexfile=index.html [L]

If you use "index.php" as your main index files, change where it says "index.html". If you use different naming schemes for different subdirectories, you can adjust for this by placing an .htaccess file in the directory where an uncommon name is used for the index file. Wherever you place the .htaccess file, that becomes the top level for that particular .htaccess file. So, now you will edit the first segment for the directory where .htaccess resides and the second segment for any subdirectory below that one. For most web sites, having multiple .htaccess files won't be needed especially if a common naming convention is used for the index files.

The third segment is used when the user actually includes a real filename in the URL. This segment will work just as it is with no editing needed.

Now, your .htaccess file is finished and ready to upload to your web site. Upload it to your main directory where your web site's main index file is located. Once you have done this, you will be blocking the countries you have selected.


Next Previous Contents