How to use Apache to create temporary and permanent redirects for your website

The path to a strong, long-lasting online presence never did run smooth. Usually, it’s only after years of work that your business really starts growing and you feel like you’ve created a great reputation for your brand that not only attracts customers and makes them come back time and again.  

But even after years of work, circumstances change and you must adapt. For instance, you might decide at some point to change your domain, for personal or professional reasons. If that happens, it’s never wise not to just abandon the old domain.

Why is it a good idea to redirect?

It’s easy to understand why: your old clients will have trouble finding you, and bookmarks and links leading to the old domain will be rendered obsolete, since their browsers will have no instructions about how to find its new location.

The ideal solution to this kind of situation is creating redirects. Changing domains without redirecting will cause your website to lose traffic from previous visitors, which in turn leads to loss of revenue and failed business opportunities.

Types of redirection

There are two most common types of HTTP redirection:

Temporary redirects –  useful if a URL temporarily needs to be served from a different location. This can great strategy when you are performing site maintenance, and you wish you let your visitors know that your website will be active again soon.

Permanent redirects –  these work rather differently, by informing the browser that it should forget the old address completely and not try to access it anymore. This is type of redirection you should opt for if your content is being permanently moved to a new location, like when you change domain names.

How is it accomplished?

On a server with Apache 2 installed and set up to serve your website(s) with virtual hosts, you can create single-page redirects by using the Redirect directive, included in the mod_alias module that is enabled by default once Apache is installed. This directive takes at least two arguments, the old URL and the new URL, and can be used to create both temporary and permanent redirects.

A temporary redirect can be accomplished by entering the following command into your server configuration:

<VirtualHost *:80>
     ServerName www.domain1.com
     Redirect / http://www.domain2.com
 </VirtualHost>
 
 <VirtualHost *:80>
     ServerName www.domain2.com
     . . .
 </VirtualHost>

However, this command works only for a single page of your website. To redirect more than one page, you can instead use the RedirectMatch directive, which uses regular expressions to specify entire directories instead of just single files.

<VirtualHost *:80>
     ServerName www.domain1.com
     RedirectMatch ^/(.*)$ http://www.domain2.com/$1
 </VirtualHost>
 
 <VirtualHost *:80>
     ServerName www.domain2.com
     . . .
 </VirtualHost>

For permanent redirection, you can append permanent to either of the directives:

 Redirect permanent / http://www.domain2.com
 RedirectMatch permanent ^/(.*)$ http://www.domain2.com/$1

 

And you’re done. Congratulations on creating temporary or permanent redirects using Apache!

Leave a Reply