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 -------------------

RewriteEngine On
RewriteCond %{REQUEST_URI} ^.*\.htm [OR]
RewriteCond %{REQUEST_URI} ^.*\.html [OR]
RewriteCond %{REQUEST_URI} ^.*\.php [OR]
RewriteCond %{REQUEST_FILENAME} -d
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 twice. 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 both references in your .htaccess file.

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.

IMPORTANT: Just because you have this in the .htaccess file in your main directory doesn't mean that your entire site is protected. If you have other .htaccess files within other directories, the Apache web server may or may not look at the main .htaccess file depending on what is contained within those other .htaccess files. However, if this is the only .htaccess file on your entire site, then the entire site is protected. Read the next chapter to find out how you can test each directory to see if they are each protected.


Next Previous Contents