Quick and Easy 301 Permanent Redirect with Apache on Ubuntu 14.04
Quick and Easy 301 Permanent Redirect with Apache on Ubuntu 14.04
Jason Jason Photo By Jason Jason, Feb 24, 2016

It's very important for your site's content to have only one official page, called the canonical URL. The reasons for this are many, but above all, it's to improve SEO so that Google will rank your page better.

Specifically with Baker Street Systems, I'd like to have all traffic be encrypted and point to https://bakerstreetsystems.com. This means that all traffic coming in as http://bakerstreetsystems.com, http://www.bakerstreetsystems.com, or https://www.bakerstreetsystems.com should be permanently redirected to the canonical domain name, https://bakerstreetsystems.com. This can be done with Apache's conf files. Here are some examples of configuration files that illustrate the correct way to permanently redirect your url:

Example bakerstreetsystems.com.conf

<VirtualHost *:80>
    ServerAdmin jj@bakerstreetsystems.com
    ServerName bakerstreetsystems.com
    ServerAlias www.bakerstreetsystems.com

    RewriteEngine on
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
</VirtualHost>

Example bakerstreetsystems.com-le-ssl.conf

Note: We use Let's Encrypt to generate SSL certificates, which is where the file name comes from. Check out How To Secure Apache with SSL Certificate from Let's Encrypt.

<IfModule mod_ssl.c>
<VirtualHost *:443>
    ServerAdmin jj@bakerstreetsystems.com
    ServerName bakerstreetsystems.com
    ServerAlias www.bakerstreetsystems.com

    <Directory "/var/www/[YOUR PATH TO WEBSITE]">
        Options FollowSymLinks
        AllowOverride All
    </Directory>

    DocumentRoot /var/www/[YOUR PATH TO WEBSITE]

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

    SSLCertificateFile /etc/letsencrypt/live/bakerstreetsystems.com/cert.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/bakerstreetsystems.com/privkey.pem
    Include /etc/letsencrypt/options-ssl-apache.conf
    SSLCertificateChainFile /etc/letsencrypt/live/bakerstreetsystems.com/chain.pem
</VirtualHost>
</IfModule>

References

http://stackoverflow.com/questions/234723/generic-htaccess-redirect-www-to-non-www

http://www.internetmarketingninjas.com/blog/search-engine-optimization/301-redirects/


Tags & Categories

Domain Name Search Engine Optimization (SEO)