Creating Apache redirects

This article explains redirects in Apache® and how to set them up.

What is a redirect?

Apache can create a redirect that points from one vhost to another vhost on the server or another external site. Common reasons for using this feature are to force http traffic to https and to move domain names.

Using the redirect directive

Apache has a redirect directive that you can add to the vhost to redirect any traffic to the specified site. Below is an example of using the directive to redirect all http traffic to https:

    `<VirtualHost *:80>
        DocumentRoot /var/www/
        Redirect /      <-----

      <VirtualHost *:443>
         DocumentRoot /var/www/
          SSLEngine on
          SSLCertificateFile /etc/pki/tls/certs/
          SSLCACertificateFile /etc/pki/tls/certs/CABundle.crt
          SSLCertificateKeyFile /etc/pki/tls/private/

The preceding example only works for that single page by establishing a temporary 302 redirect. Establish a permanent 301 redirect, by using the redirect directive as follows:

    Redirect 301 /
    Redirect permanent /