The .htaccess file is an integral part of the Linux web hosting environment and is used for a whole range of functions from creating SEO urls for your site, to redirecting people to moved pages and blocking/allowing specific ip addresses. Below are some examples of basic commands that can be added to your .htaccess file for various functions.

Enable Or Disable Directory Listings

Allowing directory listings is not advised unless you specifically wish people to download files from your web hosting server. to block(-) or allow(+) directory listings, use one of the commands below:

Options -Indexes
Options +Indexes

Redirect Parked Domains To Your Main Domain

When you have multiple domains that you wish to use for your website, most web hosting services will allow you to “park” these domains on your main hosting, which basically means that you can use these extra domains as a domain alias.

However, it has long been debated that having the same content across multiple domain names can negatively affect the ranking of your site in search engines such as Google and Bing. Therefore, if you have multiple domains you wish to register and use with your site, the most sensible option is to choose one of the domains as your primary domain and then redirect all other domains to that primary domain. This can include forcing people to the non www version of your domain, which ultimately will result in all search engines returning results for only one domain for your content.

To redirect your parked domains to your main domain, add something along the lines of the following example to your .htaccess files:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.parkeddomain.com
RewriteRule ^(.*)$ http://www.maindomain.com/$1 [R=301,L]

For multiple domains and to force remove the www’s, use [OR] with and without the www.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.parkeddomain1.com$ [OR]
RewriteCond %{HTTP_HOST} ^parkeddomain1.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.parkeddomain2.com$ [OR]
RewriteCond %{HTTP_HOST} ^parkeddomain2.com$
RewriteRule ^(.*)$ http://maindomain.com/$1 [R=301,L]

Redirect Your Domain To A Subfolder

Redirecting to a subfolder can be a very handy trick, particularly if you wish to use one of your parked domains to show different content from your main domain without needing a separate web hosting account. However, you do need to bear in mind that if you wish to use SEO urls on these site, this redirection may conflict with the code required for your SEO url functionality, so it is best used on static HTML based sites and not dynamic sites such as WordPress or Joomla.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ [NC]
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subfolder/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ [NC]
RewriteRule ^(/)?$ subfolder/index.html [L]

Use A Custom Home Page

Using a custom home page simply means that you are able to define the exact filename your website’s homepage will use.

redirect 301 /index.html http://www.yourdomain.com/home.html

Using Custom Error Pages

Using your own custom error pages allows you to create more attract error pages that can contain details instructions on what the visitor should do if they end up on one of these error pages. You can use a custom error page for any error, as long as you know its number (e.g. 404 for page not found). Just add the following to your .htaccess file:

ErrorDocument errornumber /file.html

For example, to redirect a 404 “Page Not Found” error to a custom 404.html in your root directory, you would use the entry below:

ErrorDocument 404 /404.html

If the custom error page is located in a subdirectory, just add the subdirectory path as follows:

ErrorDocument 404/subdirectory/404.html

These are some of the most common errors:

401 – Authorization Required
400 – Bad request
403 – Forbidden
500 – Internal Server Error
404 – Page Not Found

Restrict Directory/Site Access by IP Address

Restricting directory access or website access to specific ip addresses can be a very effective way to protect sensitive directories on your website(e.g. administration sections). Any directory in your public_html folder can be protected in this way. If a visitor to your site attempts to browse to a protected directory from a an IP Address that is not specified, they will simply get served a 403 Forbidden error.

In the directory you wish to protect, open/create your .htaccess file, and then add the following code to it, replacing 100.100.100.100 in the example with the static IP address you wish to allow:

Order Deny,Allow
Deny from all
Allow from 100.100.100.100

Optional: You can enter partial IP Addresses, e.g. 100.100.100, which will allow all end IP addresses in that ip range.
Optional: You can add multiple addresses by separating them with comma’s, e.g. 100.100.100.101, 100.100.100.102

NOTE: some web hosting environments limit the folder depth that the server will allow .htaccess to operate from so you may need to check with your provide if this is the case on their servers.

Comments are closed.