What is a .htaccess file and how can you create one?
- News & information
- Technic
- What is a .htaccess file and how can you create one?
An .htaccess file is a configuration file located at directory level and containing certain instructions. These instructions tell you how incoming traffic to your website will be handled.
This can be about the website access, for example via a URL redirect, but also about a URL shortening or an access control. There are a number of things that can be recorded in the .htaccess file.
The file must be supported by the various web servers in use. The .htaccess file can also adjust some settings of certain subfolders in your directory.
In this article we explain more about what a .htaccess file is (with examples) and how you can convert a .htaccess to Nginx.
Note: Hipex does not make use of .htaccess, but Nginx. This is the reason why you can also read how you can convert .htaccess to Nginx.
A file without a name
A .htaccess file is actually an extension. Those are the last few letters associated with a file name. Like an image extension file 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.
A .htaccess file is actually a file without a name that you place in your root folder or possibly in another folder.
How to create a .htaccess file?
It may sound very complicated, but you can make a .htaccess very quickly and easily yourself. You can do this for example in the notepad or notepad. A .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. When 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 a .htaccess in your root folder, it will expire if one is also placed in the subfolder. The .htaccess in the subfolder overrules the .htaccess in the root.
Examples of frequently used .htaccess lines
Below, you will find some examples of frequently used .htaccess lines:
Create a .htaccess redirect
With the codes below, it is possible to point a domain to a specific map within your hosting plan. You place the following codes in the .htaccess file:
Rewrite Engine On
RewriteRule ^$ /name-of-map[L]
If you want to refer the domain name to another domain name, then you have to use these codes:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?your-own-domain.com
RewriteRule ^(.*) http://www.your-second-domain.com/$1\[R=301,L]
This code is also an option for doing this:
Redirect 301 / http://www.your-own-domain.com
If you want to point your domain name correctly to a special map of another domain:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?your-own-domain.com$\[NC]
RewriteRule ^ http://your-second-domain.com/name-of-map\[P]
With the following code, you create a redirect and still keep the domain name:
RewriteCond %{HTTP_HOST} ^(www.)?your-own-domain.com$\[NC]
RewriteRule ^((?!name-of-map/).*)$ /name-of-map/$1\[L,NC]
Forcing the code for HTTPS
If you prefer all visitors of your website use an encrypted SSL/HTTPS, you can place a code for this in the .htaccess file.
For the entire domain, you use the following code:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 \[R,L]
If it is meant for only a part of the domain, you can add the following code into the specific maps:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} \[R,L]
Block IP-addresses
If you want to block certain IP-addresses then you use this code. Obviously, you must replace the current numbers into the numbers of the IP-address you want to block:
order deny,allow
deny from 12.34.56.78
deny from 87.65.43.21
allow from all
Deny access to .htaccess
Not only you but also others can be very curious about what you have in your .htaccess. If you don’t want someone else can see this code, then you have to insert the following code:
order allow,deny
deny from all
Errors
With an error message on a website, you often see a meaningless page. it will look better to set a self-made page for this error. To do this, you have to use this code:
ErrorDocument 403 /errors/no-access.html
ErrorDocument 404 /errors/not-found.html
Convert .htaccess to Nginx
The Nginx servers are increasingly popular and used more and more 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 a Nginx server. With Nginx servers you have to redirect http traffic to https.
What is Nginx?
Nginx is an open source web server. Besides web server, Nginx is also used as a reverse proxy, HTTP cache and load balancer. Nginx aims to provide high performance with low memory usage.
At Hipex 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 information about Nginx on our Hipex Docs >>
The benefits of HTTPS over HTTP
With HTTPS via HTTP, all data is encrypted in both directions, so it can’t be read in case it is intercepted. As a result, your website is more likely to be 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 should avoid redirecting traffic based on the if-directive. This may cause unpredictable behavior of the server.
The redirection per 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 at 443.
Do you want to redirect one website? Then read this article about redirecting from http to https in our knowledge base.
Usually you will also want to redirect the https-www version of the site to the non-www or vice versa. The recommended way to do the redirect is to create a separate server block for both www and non-www versions. You can read exactly how to do this in this article about redirecting www in our knowledge base.
After changing the configuration files, reload is sufficient, a restart is not necessary after adjusting. config.
Hipex 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 redirection for all sites
If you don’t want to create a separate http server block for each site, then you create one that will redirect all http requests to the https blocks.
To do this, you need to open the Nginx configuration file and make the following changes:
server
listen 80 default_server;
listen \[::]:80 default_server;
server\_name \_;
return 301 https://$host$request_uri;
Top 5 meest gelezen
Volg ons op social media
Hypersnelle e-commerce hosting
Snellere website = betere conversie
Probeer onze hypersnelle hosting 30 dagen gratis uit en ontvang een vergelijkingsrapport met je huidige hosting.
- Tot 30 dagen gratis uitproberen
- Performance analyse
- Vergelijkingsrapport huidige hosting
- Beste hosting van Nederland
Start 30 Day Trial