Coding & Camembert
// April 17, 2017

How to Redirect your Domain

Share

Ever need to temporarily, or permanently send visitors to a new domain without asking them to click a new link? There are ways to do this that do not even cause the page to reload. If you are using Apache and have high level SSH access, you can achieve this by editing the configuration files of the specified domains. Alternatively, you can also do a redirect by editing the .htaccess file located in the home directory of your server.

Apache Redirect

This option requires a high level of access to your server. You can try and edit the configuration files using an FTP client, however I would recommend using the command line and SSH into the server to do this. The following directions are assuming you have SSH access.

Locate your “sites-available” directory and edit the .conf file of the website you choose to redirect. Your address may look something similar to this:

sudo nano /etc/apache2/sites-available/somesite.com.conf

The file may look something like this:

<VirtualHost *:80>
    ServerAdmin some@email.com
    ServerName somewebsite.com
    ServerAlias www.somewebsite.com

    DocumentRoot /var/www/public_html
    <Directory />
        Options FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    <Directory /var/www/somewebsite.com/public_html>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

You’ll want to change the lines ServerName and ServerAlias to the following code:

Redirect /oldsite.com http://newsite.com/

If you want to redirect the entire domain, so that oldsite.com/somefolder now links to newsite.com/somefolder, simply use a forward slash instead of the old domain address.

Redirect / http://newsite.com/

Finally, depending on the type of redirect you want to give the browser (301 or 302), is where you want to add the permanent keyword to your code. If you want it to be temporary (302), leave the line as is. To be permanent, you can use either one of these:

Option 1:

Redirect permanent / http://newsite.com/

Option 2:

Redirect 301 / http://newsite.com/

.htaccess Redirect

What if you don’t have access to the configuration files? .htaccess is a much simpler, and safer option to manipulate. In the home directory of your server, you should find the .htaccess file. If you do not see it, your FTP client may not show hidden files. If the hidden files option is on and you still do not see it, you will need to create an .htaccess file and upload it. There is nothing special about this file, you simply need to name the extension as .htaccess and add the rules within it as needed.

The following rewrite rules use RegEx (regular expressions) to test the URL if it should be redirected Code 301 is a permanent redirection, whereas code 302 is temporary. If you want to redirect the entire domain,  you would want to use the following code. The single forward slash says to forward the entire directory.

Redirect 301 / http://newsite.com/

With the following code, only the index.html would be redirected. You may want to do this if you still want access to your files under the old domain. For example, this allows you to still navigate to oldsite.com/somefolder. You can use either option:

Option 1:

RewriteEngine On
RewriteRule ^(index\.html)?$ http://somesite.com [R=301,L]

Option 2:

Redirect /index.html http://newsite.com/somefolder/
Share