What is an .htaccess file and how do you create it?

An .htaccess file is a directory-level configuration file that contains certain instructions. These instructions specify how the incoming traffic to your website is handled. This can be about website access, for example via a URL redirect, but also about a URL shortening or an access control. For example, there are a number of things that can be recorded in a .htaccess file. The file must be supported by the various web servers. The .htaccess file can also adjust some settings of certain subdirectories. In this article you can read what else you can do with an .htaccess file, we give some concrete examples and we show you how to convert them to Ngnix.Note: Hypernode does not use .htaccess, but Nginx. That is why you also read how you can convert .htaccess to Ngnix.

What can you do with an .htaccess?

  • Block IP addresses
  • Customize Error Messages
  • Protect folders
  • Redirect
  • Powering up the rewrite engine

A file without a name

An .htaccess file is actually an extension. Those are the last few letters associated with a file name. Like the file of an image can end in jpg, png or gif. The file extension usually follows the name of the file, separated by a period, for example: holiday photo.jpg. An .htaccess file is actually an unnamed file that you place in your root folder or possibly in another folder.

How do you create an .htaccess file?

It may sound very complicated, but you can make an .htaccess very easily and quickly yourself. You can do this, for example, in the notepad. An .htaccess file is nothing more than a simple text file of a few lines. The lines in the file also determine the purpose of the .htaccess. Once you have created the file, place it on your hosting space in the appropriate folder. So the folder where you want to apply the rules. If there is already an .htaccess in your root folder, it will expire if one is also placed in the subfolder. The .htaccess in the subfolder overrides the .htaccess in the root.

Examples of common .htaccess rules

Below are some examples of commonly used .htaccess rules:

Create an .htaccess redirect

With the codes below it is possible to point a domain to a specific folder within the hosting package. To do this, place the following codes in the .htaccess file:

RewriteEngine On
RewriteRule ^$ /mapnaam[L]

If you want to refer the domain name to another domain name, you use these codes:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?jouw-eigen-domein.nl
RewriteRule ^(.*) http://www.jouw-tweede-domein.nl/$1\[R=301,L]

This code is also an option:

Redirect 301 / http://www.jouw-eigen-domein.nl

Do you want to redirect the domain name to a special folder of another domain:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?jouw-eigen-domein.nl$\[NC]
RewriteRule ^ http://jouw-tweede-domein.nl/mapnaam\[P]

With this code you create a redirect and you still keep the domain name:

RewriteCond %{HTTP_HOST} ^(www.)?jouw-eigen-domein.nl$\[NC]
RewriteRule ^((?!mapnaam/).*)$ /mapnaam/$1\[L,NC]

Force the code for HTTPS

For example, if you prefer that all visitors to your website use an encrypted SSL/HTTPS, you can place a code for this in the .htaccess file. You can do this for the entire domain with the following code:

RewriteEngine OnRewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 \[R,L]

If it’s only part of the domain, then you can put the following code in the specific directories.
RewriteEngine On
RewriteCond %{HTTPS!=on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} \[R,L]

Block IP addresses

If you want to block certain IP addresses, use this code. Of course you have to replace the numbers that are there now with the numbers of the IP address you want to block:

order deny,allow
deny from
deny from
allow from all

Deny access to .htaccess

Others can also be very curious about what you have in your .htaccess. If you don’t want anyone else to see this, post the following code:

order allow,deny
deny from all

Error Messages

With an error message on a website, you often see a meaningless page. It’s nicer to set up a custom page. For this you can use the following code:

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

Convert haccess to Nginx

The Nginx servers are becoming more and more popular and used as an alternative to Apache. If you choose a Nginx server, it will also affect your .htaccess code. This must then be converted so that it is compatible with an Nginx server. You have to redirect the http traffic to https in Nginx, as it were.

What is Nginx?

Nginx is an open source web server. Besides web server, Nginx is also used as reverse proxy, HTTP cache and load balancer. Nginx aims to provide high performance with low memory usage. At Hypernode we use Nginx because tests show that we achieve better performance results with it than Apache. Years ago this difference was very big but today Apache comes close to the performance of Nginx. Read more about Nginx in our knowledge base.

The benefits of HTTPS over HTTP

With https over http, all data is encrypted in both directions, so it cannot be read if it is intercepted. As a result, your website is considered safe by Google and other popular browsers.

Prevent unpredictable behavior

If you want to redirect http to https in Nginx then configuring a separate server block is preferable, for each version of the website. You must avoid redirecting traffic based on the if directive. This can cause unpredictable server behavior.

The redirection by site

If you are going to redirect http to https per site and an ssl certificate is installed on that domain, you usually have two server blocks. The site’s first server block for the http version is on port 80 and the https version has its port on 443. Want to redirect one website? Then read this article about redirecting from http to https in our knowledge base.

Usually you also want to redirect the https-www version of the site to the non-www or vice versa. The recommended way to do the redirection is to create a separate server block for both www and non-www versions. You can read exactly how you do this in this article about the redirection of www in our knowledge base. After changing the configuration files, reload is sufficient, a restart is not necessary after adjusting. config. Hypernode has the ‘nginx-reload’ command for this that customers can use. Good to mention is that this command first tests the config (for (syntax) errors) and then does a reload.

The redirect for all sites

If you don’t want to create a separate http server block for each site, create one that redirects all http requests to the https blocks. To do this, open the Nginx configuration file and make the following changes:

listen 80 default_server;
listen \[::]:80 default_server;
server\_name \_;
return 301 https://$host$request_uri;