Producing redirects for your .htaccess file
If you’re running Apache, you have a .htaccess file.
The .htaccess file, if maintained correctly, can be a considerable asset for any technical SEO, given that it can do many things ranging from improving TTFB, handling redirects, and setting page canonicals for non-HTML documents.
When handling redirects, a lot of enterprise and older businesses that have gone through several digital transformation phases will likely have a disorganised .htaccess file, with common redirect issues being:
- Redirect chains caused by a rule in the .htaccess file, but an additional rule implemented elsewhere.
- Old redirects not being removed when new ones have been added, again causing chains and other conflicts.
- A lack of annotation and comments between sections (please annotate your .htaccess, your future self and future .htaccess delvers will love you endlessly).
- Redirects from non-trailing slash to trailing slash being done URL by URL.
- Redirects from HTTP to HTTPS being done URL by URL.
- Blanket HTTP to HTTPS rules implemented, which causes chains.
What is a .htaccess file?
The .htaccess file is a configuration (config) file used by Apache Web Servers.
Htaccess is short for Hypertext Access, and it is a configuration file used by apache-based web servers. Configuration files configure the initial settings of a program, or in this case, the server (One.com).
Producing redirects for the .htaccess file
If you have your redirects already in a CSV with the traditional too and from columns, you can use our bulk redirect generation tool. However, if you want to better understand the file, and how you can maintain these redirects over time, please read on.
Apache uses two modules to map URLs (map URL A to URL B as a redirect), these being mod_rewrite
and mod_alias
. Both have very different uses, and you’re more than likely only ever going to need to use mod_rewrite
.
mod_alias
mod_alias
can be used to handle redirects, as it does contain the Redirect
syntax, and this takes the form of:
# Redirect [status] [URL-path] URL
So you would produce:
Redirect 301 "/old-url.html" "/new-url.html"
It’s important to note, however, that the status argument is optional, and you can also use a non-numeric argument, such as:
- Permanent instead of 301
- Temp instead of 302
- See Other instead of 303
- Gone instead of 410
In place of Redirect 301 and Redirect 302 you can also use RedirectPermanent
and RedirectTemp
.
Other important considerations:
- The URL-path must contain the initial /, e.g. “/banana.html” and not “banana.html”.
- The URL-path must not contain the scheme and the hostname.
- If the status argument is between 300 and 399, the URL-path also must be present.
- If the status argument is not between 300 and 399, the URL-path also must not be present.
mod_rewrite
As an SEO, understanding the complete intricacies of mod_rewrite isn’t necessary, but having a top-line understanding of the main directives and arguments is extremely useful and productive when maintaining a .htaccess file.
The five main directives you want to know about are:
- RewriteEngine
- RewriteBase
- RewriteOptions
- RewriteCond
- RewriteRule
Out of these, RewriteCond and RewriteRule will do most of the heavy lifting and achieve most of your goals and dreams.
The RewriteCond
directive specifies a condition under which the RewriteRule
that proceeds will be executed; this consists of the directive, a TestString, a CondPattern, and then [flags].
The RewriteRule
is the foundational element of mod_rewrite. It is what is used to manipulate URLs and create redirects.
In context of a regular redirect, this will look like:
# 301 — http://www.source.com/old.html => http://www.destination.com/new.html
RewriteCond %{HTTP_HOST} ^www\.source\.com$
RewriteRule ^old\.html$ http://www.destination.com/new.html? [L,R=301]