Skip to main content
TrustServers Knowledge Base

Tip: Start typing to get instant search results.

Table of Contents

Practical Examples of Using the .htaccess File

Hypertext Access (.htaccess) files are configuration files that contain directives for the web server, allowing you to define how it behaves. Rules can be set in these files without requiring access to the server’s central system configuration.

On all our servers we choose to use the NGINX web server. On our managed hosting plans that run with a hosting control panel (such as shared hosting, semi-dedicated hosting, etc.), we use both NGINX and the Apache web server with the LiteSpeed PHP API module, which makes it possible to take advantage of .htaccess files. On our dedicated servers, which run exclusively on NGINX, we take care of properly converting your .htaccess rules into NGINX rules in the most optimal way possible.

Each subfolder can have its own .htaccess file with separate rules. The main .htaccess file is located in the root folder containing the website files (e.g., public_html folder). If the file does not exist, you can create it. If it already exists, you can add the extra rules (often added at the top of the file).

Below are some useful examples of how to make use of the .htaccess file.

Redirect

There are various reasons why you might need to set up redirects on your website. Here are some useful examples.

Redirect all URLs of the old domain to the homepage of the new one

When changing your website’s domain, it is important to redirect all URLs of your old domain to the homepage of the new one. For example, suppose you want to redirect all old URLs from oldsite.org to the homepage of newsite.org. Simply add the following line to your .htaccess file.

Redirect 301 / https://newsite.org/

This way, https://oldsite.org/test/page.html for example, will redirect to https://newsite.org/

Redirect all old URLs to their corresponding URLs on the new domain

If when changing your website’s domain there is a one-to-one correspondence between the old and new URLs, you can redirect all old URLs to their matching URLs on the new domain. To do this, simply add the following lines to your .htaccess file.

# Redirect oldsite.org URLs to newsite.org
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^oldsite.org$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldsite.org$
RewriteRule (.*)$ https://www.newsite.org/$1 [R=301,L]
</IfModule>

This way, https://oldsite.org/test/page.html for example, will redirect to https://newsite.org/test/page.html

Redirect a website subdirectory to a new subdomain

Suppose you want to redirect all requests made to the blog/ subdirectory to a new subdomain, such as blog.example.org. Simply add the following lines to your .htaccess file.

# Redirect /blog/* to blog subdomain
RewriteRule ^blog/(.*)$ https://blog.example.org/$1 [R=301,L]

This way, https://example.org/blog/page.html for example, will redirect to https://blog.example.org/page.html

Redirect non-www domain to www

For search engines, mysite.org and www.mysite.org are considered two different websites. If the page loads under both versions without a redirect, Google may get confused about which version to index, potentially resulting in a duplicate content penalty that negatively affects your rankings (SEO).

It is therefore important to ensure that your website always loads with the www prefix (or without it). To do this, simply add the following lines to your .htaccess file.

# Redirect non-www to www
<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteCond %{HTTP_HOST} !^www\. [NC] 
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
</IfModule>

Block an unwanted IP address

There are often situations where you need to block access to your website from a malicious IP address. This can easily be done with a single entry in the .htaccess file.

For example, suppose you want to block two IP addresses: 203.0.113.68 and 198.51.100.23. Simply add the following lines to your .htaccess file.

# Block selected IPs
# Apache 2.4+
<RequireAll>
Require all granted
Require not ip 203.0.113.68
Require not ip 198.51.100.23
</RequireAll>

Block an unwanted bot

Another useful application of the .htaccess file is blocking bots. For example, suppose you want to block the bots Jyxobot and SBIder, which send the User-Agent strings “Jyxobot” and “SBIder” respectively. The following lines are all you need.

# Block some bots
<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteCond %{HTTP_USER_AGENT} "Jyxobot|SBIder" [NC] 
RewriteRule .* - [F,L] 
</IfModule>

This way, we can block bots that are either malicious or making an excessively high number of requests to our website in a short period of time, causing resource exhaustion. That said, it is important not to block “good” bots such as Google’s, as their access to our website is essential for proper indexing and visibility.

Was this article helpful?
Please Share Your Feedback
Πώς μπορεί να βελτιωθεί το άρθρο;
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.