Redirecting with mod_rewrite

Assume you have a shiny new site with great content, a new URL (or http address) and are expecting customers. Unfortunately your old site is not yet really gone in the search results but luckily you still have access to your old address. There is an easy way to redirect the users, who click on the search results pointing to the old page.

1. Setup a vhost

You need to answer to those requests. For this step it is assumed that the old domain is pointing to a web space or server managed by you. Basically all you need is a folder and a configured host (vhost):

<VirtualHost *:80>
  ServerName my.old.domain
  DocumentRoot /var/www/myoldsite
  <Directory /var/www/myoldsite/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Require all granted
  </Directory>
</VirtualHost>
<VirtualHost *:443>
  ServerName my.old.domain
  DocumentRoot /var/www/myoldsite
  <Directory /var/www/myoldsite/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Require all granted
  </Directory>
  SSLEngine on
  # SSL configuration
</VirtualHost>

Let’s just answer on both ports. Just in case. And don’t forget to enable mod_rewrite:

a2enmod rewrite

Now if the page is opened, there is no error but a empty directory listing. Better than nothing, but the goal is to redirect to the new page.

Hint: I am realizing this with a “.htaccess” file because this way I can reconfigure “on the fly” and do not have to reload the web server after every change.

2. Setup “.htaccess” for redirection

Create a new file in the directory ” /var/www/myoldsite” named “.htaccess”. Don’t forget the leading “.”!

RewriteEngine on
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule .* https://my.new.site

This means: If the url contains a query string (the part after the top level domain) rewrite the whole URL to ” https://my.new.site”.

Why?

You don’t know which sub pages are cached and shown in the search results. So: It’s better to just forward all to the new page instead of showing a “404 – not found”. Plus: You can define multiple rules and forward specific matches to possible new sub pages. But that would be beyond the scope of this short tutorial.

Why just sub pages?

On the main page I show a hint, that the URL has changed and that the user should be so kind as to change his bookmarks if the old URL is stored there. He is automatically redirected to the new page after 7 seconds:

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="refresh" content="7; url='https://my.new.site'" />
    <style type="text/css">
        * { font-family: sans-serif; }
    </style>
  </head>
  <body>
    <p>The domain has changed to 'https://my.new.site'. If you have bookmarks, please save them under the new domain. You will be forwarded in a few seconds. Alternatively you can use <a href="https://my.new.site">this link</a>.</p>
  </body>
</html>

I hope this helps somebody to guide users and customers to a new and improved web presence without having to deal with ugly error messages and confusion when clicking on search results.