Tag Archives: apache2

Apache: Simple way to Force a Domain Redirect Tips, Tricks and Tutorials 22 JUL 2014

I was decommissioning an old server the other day, and in the process of moving the system to a new server I learned that the old domain was in fact one that we didn’t directly control. In other words, time to enforce a new domain on our clients! Of course, getting anyone to change URLs or domains on their personal machines is never an easy thing, so of course there has to be a small period of playing nice, in which you need to seamlessly redirect from one domain to the other. In other words, if an old URL is oldserver.com/about/ then I want anyone hitting that URL to be redirected to the new URL at newserver.com/about/.

To do this on your LAMP server is pretty easy thanks to the powerful mod_rewrite Apache module. Simply open up your relevant vhost declaration file in /etc/apache2/sites-available and add the following to the bottom of it (obviously using your own domains in place of the sample ones):

<IfModule mod_rewrite.c>
rewriteEngine on
rewriteCond %{HTTP_HOST} ^oldserver.com$
rewriteRule ^(.*) http://newserver.com$1 [L,R=301]

Reload your apache configuration with

sudo service apache2 reload

and try to hit “oldserver.com/about” now.

Works well, doesn’t it? (And it wasn’t even difficult to do!)


[UPDATE: Lately I’ve switch to placing this in the virtual host file:]

<VirtualHost *:80>
Options +FollowSymLinks
RewriteEngine on
RewriteRule .? http://newserver.com%{REQUEST_URI} [L,R=301]

Ubuntu: How to set up an Apache Virtual Host (vhost) CodeUnit 28 SEP 2011

To set up a virtual host (vhost) under Apache on your Ubuntu server is relatively simple. First you need to create a config file under the /etc/apache2/sites-available directory. A good practice is to name this file the same as the folder you want to use to hold this virtual host’s web pages.

For this example we want to serve up traffic coming in for www.mydomain.com, so we create a file called mydomain:

sudo nano /etc/apache2/sites-available/mydomain

The contents of which looks like this:

<VirtualHost *:80>
DocumentRoot /home/www/mydomain
<Directory />
Options FollowSymLinks
AllowOverride None
<Directory /home/www/mydomain>
Options FollowSymLinks
AllowOverride None
Order allow,deny
allow from all
ServerName mydomain.com
ServerAlias www.mydomain.com

ErrorLog /var/log/apache2/mydomain-error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog /var/log/apache2/mydomain-access.log combined

ErrorDocument 404 /error_documents/404.php
ErrorDocument 403 /error_documents/403.php


With the file saved, next we enable the site by running:

sudo a2ensite mydomain

Finally, reload the Apache configuration with:

sudo /etc/init.d/apache2 reload


(And if you are in the process of looking for a hosting server, you may want to check out different web hosting companies first to help you compare and find the most appropriate hosting account for you.)

Ubuntu: How to Enable or Disable an Apache2 Site / Virtual Host CodeUnit 07 JAN 2011

Sites or virtual hosts are extremely useful when you are hosting more than one website on a box. I’ve already shown you how to set up virtual hosts under Apache 2, but I thought I would quickly just give a little more insight into what the a2ensite and converse a2dissite function calls do.

All that a2ensite is, is a script that enables specified sites (which contain a
block) within the apache2 configuration.

It achieves this by creating symlinks within /etc/apache2/sites-enabled. The opposite of doing this is a2dissite which disables the site by removing that symlink.

Funnily enough, you can enable or disable a site multiple times without getting an error, simply because all these scripts are doing is manipulating symlinks. Lastly, the default site is handled specially, with a resulting symlink called 000-default in order to be loaded first.

And now you know.