How to Redirect a URL in a Virtual Host

By | 2013/01/18

Redirecting a URL in a virtual host is a snap! I’ll show you how.


Scenario: you have a server that receives example.com but you want that to redirect to example2.com.

First you will want to add this to your /etc/apache2/ports.conf (Debian or Ubuntu) or /etc/httpd/conf/httpd.conf (Red Hat or CentOS) if you have not already:

NameVirtualHost *:80


Next, setup a virtual host with the following to have it redirect to an alternate URL:

<VirtualHost *:80>
      ServerName www.example.com
      ServerAlias example.com www.example.com
      Redirect 301 / http://www.example2.com/
</VirtualHost>

Done! When a user visits example.com they will be redirected to http://www.example2.com