.htaccess Files

An .htaccess file is a powerful tool to help you get the most our of your hosting services. This tool can be used modify the default behaviors of your web server, to an extent. While it has several uses that power users will be able to use, it can also be used to redirect web site traffic, either to different directories, or even to completely different servers.

Let us take a look at .htaccess files and how they can be used with your Deluxe Hosting web hosting services.

What is an .htaccess file

An .htaccess file is a hidden file (any file beginning with a period is hidden by default in UNIX systems) that can be used to modify the default behavior of a web hosting server.

With a shared hosting service, your account is on a server that is also used by several other users. Sharing the server resources helps reduce the cost of hosting by spreading the hardware costs across several accounts, as most websites require relatively little server resources. The down side of shared hosting is that the server must be configured in a way that works for most users, with some more advanced features disabled by default. It is also not feasible to configure each account on the server level.

Luckily, you can tweak your web hosting service using an .htaccess file. While this file is not able to override all server functions, it can be used to customize your environment. You can also use it to configure individual folders.

What can I do with an .htaccess file

While there is too much to mention it all here, the most common use of .htaccess files is for redirecting website traffic.

Whether you are looking to redirect to a different folder then the document root folder (if your website is in a subfolder), or perhaps your home page is named something other then the default index.html file, you can set these with an .htaccess file.

And not just for redirects. If you are using a CMS (Content Management System) to manage your website, such as WordPress or Joomla, they will also include an .htaccess file during installation. In this case, the .htaccess file is used to set up the environment that the CMS needs to function correctly.

Another common use of .htaccess files is to force web traffic to use the HTTPS protocol instead of the unencrypted HTTP. This is common with websites that are protected with SSL certificates. An SSL protected site will encrypt any data transferred between the server and the end user to prevent any third parties from being able to intercept the data for their own diabolical use. This is quickly becoming the standard for web sites. Find out more on SSL certificates here.

The .htaccess file can be a powerful tool in the right hands. If you would like to learn more about the features of the .htaccess file, check out this link to the Apache website.

How do I use an .htaccess file

An .htaccess file is a simple text based file. This means that you can edit it with any text based editor:

  • any text editor on you computer, such as Notepad (WordPad or Word are not recommended as they include much more processing than needed)
  • the text editor included with cPanel will work great
  • most FTP clients include a text editor, these should function fine
  • if you are using SSH, you can use your preferred text editor, such as nano or vi

The main thing to look for is a basic text editor that does not do a lot of auto correction or formatting. You want an app that will save exactly what you enter without altering it in any way.

If you do not have an .htaccess file in your directory, it either does not exist or is hidden. Remember, any files that begin with a period (dotfiles) are hidden by default. Depending on what editor you are using, you should be able to enable the viewing of hidden files.

In the cPanel File Manager, you can show hidden files by clicking on Settings in the upper right-hand side of the screen.

In the Settings menu, make sure that Show Hidden Files (dotfiles) checked. If you still do not see the file, you can create a new one.

Once you create or open your .htaccess file, you will see that it is just written in plain text. Here is an example of a WordPress htaccess file:

# BEGIN WordPress

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]


# END WordPress

While we cannot go over everything you can do with the .htaccess file, we can go over some of its most common uses.

Common .htaccess usage

Here are a few common reasons to add an .htaccess file to your document root folder.

Any line that begins with “#” is considered a comment and is not executed by the server. This is mostly used to describe what the code doing and for organization.

You can force all website traffic to the SSL secured protocol using this:

# Redirect to HTTPS
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^domain.com$ [OR] RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteRule ^(.*)$ https://www.domain.com/$1 [R,L]

You only need to replace domain.com to your domain name.

If you want to use a file other then the index.html file as your home page:

# Change default website page
DirectoryIndex home.php

Change home.php with the file you want to load first.

Your website may be in a subfolder of your document root. If so, use this:

# Change default home directory
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com$ [NC,OR] RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteCond %{REQUEST_URI} !folder/
RewriteRule (.*) /folder/$1 [L]

Change domain.com with your domain and folder with your target folder.

You can use the .htaccess file for several different types of domain redirects. Such as:

Redirect an entire site
Redirect 301 / https://www.domain.com/

Replace the target with your target URL.

Redirect an entire site to a sub folder

Redirect 301 / https://www.domain.com/subfolder/

Enter the target URL and subfolder.

Redirect a sub folder to another site

Redirect 301 /subfolder https://www.domain.com/

Enter both the subfolder you wish to redirect and the target.

Redirect a single page

Redirect 301 /pagename.php https://www.domain.com/pagename.html

Provide both the page to redirect and the new target.

You can also forward all of the website traffic to a different URL.

# Domain Forwarding
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example-old\.com$ [NC] RewriteRule ^(.*)$ https://www.example-new.com/$1 [R=301,L]

Replace example-old and example-new with the relative domains.

You can use the htaccess file to restrict certain IP addresses or ranges.

# Block IPs
order allow,deny
deny from 192.168.42.69 //Some hacker
deny from 10.0.0.1/10.0.0.255 //A range of IPs
allow from all

This will block the IP 192.168.42.69 from accessing your site, or anyone using the IPs from 10.0.0.1 to 10.0.0.255.

You can use the same method to allow only certain IPs.

# Allow IPs
order deny,allow
deny from all
allow from 192.168.42.69 //My Home IP
allow from 192.168.45.36 //My Web Developer

This will only allow these IP addresses to access your site.

The text after the “//” is also a comment and does not effect execution.

If you are still having issues, and need further support, please reach out to our support team using any of these methods.

Recent Articles

What is Cloud Hosting

You probably have heard a lot of buzz around the internet about "the cloud". But what is the cloud that everyone is so excited about, and what does it have to do with your [...]