A .htaccess file (with a dot!) is a file type with no name. It is used for Web sites running on an Apache server. You can use it to execute certain things and change or bypass some (server) settings. Below we give you some examples of .htaccess rules that are commonly used. Before some things need to be "rewritten" but first the general stuff.

Blocking certain IP numbers
If you want to block an ip number to the site then do so with the following rules:

order deny,allow
deny from 12.34.56.78
deny from 78.56.34.12
allow from all

Order is : first all deny's are executed and only then the allows. Thus, first the indicated IP numbers are blocked and the others are allowed.

Block access .htaccess
To prevent people from viewing your .htacces, you can block access. You then add the following script:

order allow,deny
deny from all

The above rules apply specifically to the .htaccess file. Of course, one can also protect other files and folders using the same rules.

Define the index file
When querying the root or a specific folder, the server will search for the index file. This is the file to be displayed if no file is defined. For example: www.mijndomein.nl/uwfolder/. In most cases, servers have the following files set up: index.html, index.htm, default.asp (IIS), index.php (Apache). You can adjust this in the following way

DirectoryIndex willekeurig.html

Custom error messages
Most servers have set their default page for error messages. If you want to customize this to your own look-and-feel (html page) of, say, a 404 page, you can do so in the way below :

ErrorDocument 403 /fouten/geen-toegang.html
ErrorDocument 404 /fouten/niet-gevonden.html

Basic redirects
You can set some basic redirects in .htaccess. You can attach conditions to these or not. For conditions you use regular expressions . Here are some examples

Redirect 301 /uwoudebestand.php http://www.mijndomein.nl/nieuwemap/uwnieuwebestand.php

Make sure that the second address is a complete address, otherwise this will not work. The 301 is the redirect code for a permanent redirect.

Turn on the rewrite engine
The previous ones were the basic setting. Now the rewrite engine can be turned on. These rules allow you to do a rewrite/redirect based on some data (hostname, referer, filename, querystring, method). To turn on the engine, use the following line:

RewriteEngine on

One host name for your website
It may be desirable for SEO reasons to have your website available on 1 hostname. This will prevent duplicate content from occurring because your website will be indexed on domain A and domain B. Also, you don't want your website to be indexed on both www.site.nl and site.co.uk. To avoid this, add the following lines

RewriteCond %{HTTP_HOST} !^www\.mijndomein\.nl [NC]
RewriteRule ^(.*)$ http://www\.mijndomein\.nl/$1 [L,R=301]

This indicates: if the hostname is not (the exclamation mark stands for: not) www.mijndomein.nl then a 301 redirect should be done to www.mijndomein.nl. With a few additional settings like [NC], you say the case (upper/lower case) can be ignored. With [L] you say that this last line should be executed immediately. The [R=301] indicates that it should not be a rewrite but a redirect of type 301 (permanent).

Old domain/website redirects
The above rules are meant to perform a redirect only if the old domain is linked to the new server in the DNS. As a result, all requests will be handled on 1 server. If this is not possible, you can place a redirect on the old server. This redirect then looks like this :

RewriteRule ^(.*)$ http://www\.mijndomein\.nl/$1 [L,R=301]

Extreme traffic redirects
It may happen that your website is linked in a place that does not make you happy because it costs a huge amount of data traffic and brings visitors to your website that do not add value. In this case, place some rules to subtly redirect all this traffic:


RewriteCond %{HTTP_REFERER} deverkeerdesite\.nl [NC]
RewriteRule ^(.*)$ http://www.google.nl [L,R=302]

Visitors who are then directed to your website from deverkeerdesite.nl will now be redirected to Google. This can be just the difference between an overload (server overload) and staying online.

Redirecting from http to https
The code below will redirect all your traffic destined for http: to https:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Note this will redirect using the 301 'permanently moved' redirect, which will help transfer your SEO rankings.

Rewrite everything but...
In most cases, the rewrite engine is used to rewrite so-called neat urls behind the scenes to 1 file that handles everything else. For example, you can internally rewrite the address /folder/map2/file/ to index.php?folder/map2/file/:

RewriteRule ^(.*)$ /index.php?$1 [L]

You can attach certain conditions to this: rewrite all urls unless it is an existing folder or file. You can use this to have links to PDF files, for example, not rewritten but simply ignored:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [L]

Rewrite a particular folder
You can also redirect or rewrite 1 particular folder. You then use a condition to limit the rule to 1 folder:

RewriteCond %{REQUEST_URI} ^/deze_map/ [NC]
RewriteRule ^(.*)$ /index_voor_map.php?$1 [L]

and in addition there are the following 2 lines:

RewriteCond %{REQUEST_METHOD} GET
RewriteCond %{REQUEST_URI} !^/nog_een_map/

These rules say that the request method must be GET (you can of course use POST here as well) and that the folder /yet_a_folder/ can be ignored. Note here the exclamation point which means 'not'.

Multiple conditions
If you want to use multiple conditions, you can header them with the OR:

RewriteCond %{REQUEST_URI} ^/map1/$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/map2/$ [NC]

In conclusion

Above we have explained to you some basic redirect and rewrite rules which you can extend and combine where possible. Hoping we have helped you on your way with this.