Hostwinds Tutorials

Search results for:


Table of Contents


What is the .htaccess File?
Enabling the .htaccess File
Step 1: Open Virtual Host File
Step 2: Add 'AllowOverride All' directive
Step 3: Save File
Step 4: Restart Apache
Creating an .htaccess File
Redirecting a Single URL Using 'Redirect' Directive
If you want to redirect one URL to another URL on the same domain:
If you want to redirect a URL to one on different domain:
Bulk URL Redirect Using 'RewriteRule' Directive
Check if 'mod_rewrite is enabled
For Red Hat-based systems (CentOS, Fedora, RHEL):
For Debian-based systems (Ubunutu, Debian, Linux Mint):
How to Enable 'mod_rewrite'
For Red Hat-based systems (CentOS, Fedora, RHEL):
For Debian-based systems (Ubunutu, Debian, Linux Mint):
Redirecting URLs based on a Common Pattern
Redirecting an Entire Domain to a New Domain
Force HTTPS with 'RewriteRule' Directive
Force HTTPS on All Traffic
Force HTTPS on Specific Domain
Adding and Removing 'www' Prefix

How to Create Redirects Using .htaccess File

What is the .htaccess File?
Enabling the .htaccess File
Step 1: Open Virtual Host File
Step 2: Add 'AllowOverride All' directive
Step 3: Save File
Step 4: Restart Apache
Creating an .htaccess File
Redirecting a Single URL Using 'Redirect' Directive
If you want to redirect one URL to another URL on the same domain:
If you want to redirect a URL to one on different domain:
Bulk URL Redirect Using 'RewriteRule' Directive
Check if 'mod_rewrite is enabled
For Red Hat-based systems (CentOS, Fedora, RHEL):
For Debian-based systems (Ubunutu, Debian, Linux Mint):
How to Enable 'mod_rewrite'
For Red Hat-based systems (CentOS, Fedora, RHEL):
For Debian-based systems (Ubunutu, Debian, Linux Mint):
Redirecting URLs based on a Common Pattern
Redirecting an Entire Domain to a New Domain
Force HTTPS with 'RewriteRule' Directive
Force HTTPS on All Traffic
Force HTTPS on Specific Domain
Adding and Removing 'www' Prefix

What is the .htaccess File?

The .htaccess file is a configuration file used primarily by Apache web servers to control various aspects of website behavior on a per-directory basis. It allows you to override server-wide settings and apply specific configurations to individual directories without directly modifying the main server configuration file.

The .htaccess file stands out as a remarkably powerful tool in managing various backend website tasks efficiently. One of the more common tasks is implementing redirects, where you can execute redirects at the level of individual URLs, across entire domains, and even on the HTTP protocol level.

In this tutorial, we'll go over the myriad options .htaccess offers for redirection, walking you through the various implementations so you can effectively redirect your URLs with confidence.

Enabling the .htaccess File

Enabling the .htaccess file tells Apache web servers to recognize and accept directives written within it, which will set you up to override default server configurations at the directory level.

If you have a shared hosting account, your hosting service provider should already have it enabled, so all you have to do is create a file.

Step 1: Open Virtual Host File

Using your preferred text editor (we'll use nano for this example), open your website's configuration file with the following command:

For Red Hat-based systems (CentOS, Fedora, RHEL):

sudo nano /etc/httpd/conf/httpd.conf

For Debian-based systems (Debian, Ubuntu, Linux Mint):

sudo nano /etc/apache2/sites-available/domain_name.conf

The VirtualHost file block will look something like this:

<VirtualHost *:80>
    ServerAdmin webmaster@example.com
    ServerName domain_name
    ServerAlias www.domain_name
    DocumentRoot /var/www/domain_name/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Note: Depending on how your website files are organized, the 'DocumentRoot' may differ. There are two common setups:

  • '/var/www/domain_name/public_html' (above) indicates that your website files are in a subdirectory called 'public_html' within the 'domain_name' directory, a structure commonly used to separate public-facing files from other site-related files. Enabling the .htaccess file in this directory will only affect the files within 'public_html'.
  • Example:
/var/www/domain_name/
├── public_html/
│   ├── index.html
│   ├── about.html
│   └── .htaccess
└── logs/
  • '/var/www/domain_name' indicates that all your website files are located directly in the 'domain_name' directory. This setup means that all .htaccess file directives will apply to every file (public-facing or not) within this directory.
  • Example:
/var/www/domain_name/
├── index.html
├── about.html
└── .htaccess

While both are valid, it's important to be aware of what site files you may or may not want to be affected by the .htaccess file.

Step 2: Add 'AllowOverride All' directive

Within the VirtualHost block add the following directory content block:

<VirtualHost *:80>  

<Directory /var/www/domain_name/public_html>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>

</VirtualHost>

This directive, specifically 'AllowOverride All' is what allows you to apply .htaccess directives across all the website files within the given directory.

Step 3: Save File

After adding the directory block, save and close the file. In nano, this can be done by pressing CTRL +  X, then Y and pressing ENTER.

Step 4: Restart Apache

For the full enablement of the .htaccess file, you'll need to restart Apache:

For Red Hat-based systems (CentOS, Fedora, RHEL):

sudo systemct1 restart httpd

For Debian-based systems (Debian, Ubuntu, Linux Mint):

sudo systemct1 restart apache2

That's it! Apache will now allow you to use .htaccess files within the given directory.

Creating an .htaccess File

With support for .htaccess enabled, we can now create the .htaccess file.

To create the .htaccess file, you need to go to the root directory where you enabled the .htaccess file. Using your preferred text editor (again, we'll use nano) input the following command, replacing 'domain_name' with your domain name.

Note: Remove 'public_html' if you did not enable the .htaccess file in that subdirectory.

sudo nano /var/www/html/domain_name/public_html/.htaccess

You've now created an .htaccess file and can start laying out redirect directives.

Redirecting a Single URL Using 'Redirect' Directive

The 'Redirect' directive is the most straightforward option for simple 1-to-1 redirects. This directive can be used to redirect URLs on the same domain or to a different domain.  The following directives will work for any 3xx status codes (301, 302, 307, and 308)

If you want to redirect one URL to another URL on the same domain:

Redirect 301 "/original_url_path" "/new_url_path"

If you want to redirect a URL to one on different domain:


Redirect 301 "/orignal_url_path" "https://new_domain.com/new_url_path"

Bulk URL Redirect Using 'RewriteRule' Directive

The 'RewriteRule' directive allows you to add additional rules to redirects, such as pattern matching, through the use of regular expressions and conditions.

Check if 'mod_rewrite is enabled

In order to use the 'RewriteRule', we need to ensure the 'mod_rewrite' module is enabled.

For Red Hat-based systems (CentOS, Fedora, RHEL):
httpd -M | grep rewrite
For Debian-based systems (Ubunutu, Debian, Linux Mint):
apache2ctl -M | grep rewrite

If you see the following output, the 'mod_rewrite' module is enabled

rewrite_module (shared)

How to Enable 'mod_rewrite'

For Red Hat-based systems (CentOS, Fedora, RHEL):

Step 1: Open Apache configuration file (usually 'httpd.conf' or 'apache2.conf')

sudo nano /etc/httpd/conf/httpd.conf

Step 2: Ensure the following line is not commented out (no # at the beginning). If it is, simply delete '#'

LoadModule rewrite_module modules/mod_rewrite.so

Step 3: Restart Apache to apply changes

sudo systemctl restart httpd
For Debian-based systems (Ubunutu, Debian, Linux Mint):

Step 1: Enable 'mod_rewrite' module

sudo a2enmod rewrite

Step 2: Restart Apache to apply changes

sudo systemctl restart apache2

Redirecting URLs based on a Common Pattern

If you have a common pattern in your old URLs, such as the same subdirectory (ex. /blog)  you can use regular expressions to match and redirect them:

RewriteEngine On

RewriteRule ^old-section/(.*)$ http://www.example.com/new-section/$1 [R=301,L]

In this example, any URL that starts with 'old-section/' will be redirected to 'new-section/' with the same suffix.

Redirecting an Entire Domain to a New Domain

The following directive will redirect all traffic from 'old-domain.com' to 'new-domain.com,' preserving the URI path.

RewriteEngine On

RewriteCond %{HTTP_HOST} ^old-domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.new-domain.com/$1 [R=301,L]

Force HTTPS with 'RewriteRule' Directive

When you "forces HTTPS," it means that you're redirecting any incoming HTTP requests of your site to the secured HTTPS version.

There are a couple of ways to force HTTPS using the 'RewriteRule' directive:

Force HTTPS on All Traffic

When you force HTTPS on "all traffic," you are telling the server to redirect every incoming HTTP request to HTTPS, regardless of the domain or subdomain.

RewriteEngine On

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

This method of forced HTTPS is ideal for servers hosting a single domain.

Force HTTPS on Specific Domain

When you force HTTPS on a specific domain, you are telling the server to redirect HTTP requests to HTTPS only for that particular domain (or subdomain).

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(www\.)?specific-domain\.com$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Forcing HTTPS with this method is particularly useful when hosting several domains on a single server and you only need to secure a select number.

Adding and Removing 'www' Prefix

You can also use the .htaccess file in conjunction with the 'RewriteRule' directive to redirect 'www' prefix.

To Add 'www' Prefix:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]

To Remove 'www' Prefix:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]

Written by Hostwinds Team  /  June 3, 2024