Hostwinds Tutorials
Search results for:
Table of Contents
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 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.
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/
│ ├── index.html
│ ├── about.html
│ └── .htaccess
└── logs/
/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.
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.
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.
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.
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.
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)
Redirect 301 "/original_url_path" "/new_url_path"
Redirect 301 "/orignal_url_path" "https://new_domain.com/new_url_path"
The 'RewriteRule' directive allows you to add additional rules to redirects, such as pattern matching, through the use of regular expressions and conditions.
In order to use the 'RewriteRule', we need to ensure the 'mod_rewrite' module is enabled.
httpd -M | grep rewrite
apache2ctl -M | grep rewrite
If you see the following output, the 'mod_rewrite' module is enabled
rewrite_module (shared)
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
Step 1: Enable 'mod_rewrite' module
sudo a2enmod rewrite
Step 2: Restart Apache to apply changes
sudo systemctl restart apache2
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.
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]
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:
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.
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.
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