Your browser (Internet Explorer 6) is out of date. It has known security flaws and may not display all features of this and other websites. Learn how to update your browser.

301 Permanent Redirect of one Domain name to another using Server Alias

I am so used to using cpanel to configure domain names for my applications on shared host until I have to use a cloud based hosting and I am forced to use the terminal (though my local default OS is Ubuntu). Doing a 301 permanent domain name redirection seems more like a problem as I find limited resources online. Probably because my search word is not the right combination or maybe people never had such problem. Anyway I have put this together for someone that would be in my shoes.

I have a simple problem that led me to this. We have two domain names for one application. Both A records of the domain names pointing to same IP and the virtual host configuration pointing to same path. Now we have a facebook login for the application and it is just one domain name you can register with facebook. Even if we decided to create a separate application on facebook we would need to switch logic for each login url to use the app settings specific to it from facebook. The the need comes for us to do a permanent redirect from a domain to the domain we registered with facebook.

Below was how I got around it.

1. Make sure both domain names have same name server record.

2. Log into your server and edit the default site configuration file ( /etc/apache2/sites-available/default ) with your favourite editor.

3. I assume your site configuration looks like this (with .htaccess enabled). You might have a slight changes without htaccess (having AllowOverride as none).

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/path/to/application
                Options FollowSymLinks
                AllowOverride All
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride All
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        ErrorLog ${APACHE_LOG_DIR}/error.log
        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride All
        Order deny,allow
        Deny from all
        Allow from ::1/128

4. Add the line after the default configuration. Change the Server Alias and Redirect domain names respectively.

        ServerAdmin webmaster@localhost
        Redirect permanent /

5. Save the configuration.

6. Restart your apache service by typing this command in your terminal.

sudo service apache2 restart

7. You are good to go.


Hope this helps someone greatly.

comments powered by Disqus