45Squared

301 Redirects for Apache2

SEO is quite important for getting your business seen on the world wide web. But what do you do when you finally get around to adding an SSL certificate to your website? (PS: If you don’t have an SSL certificate you need to get one.) According to Google, not utilizing the proper redirection can negatively affect your SEO rankings. How do you mitigate that problem? A 301 Redirect. Let’s go over how to implement this for Apache2.

This guide assumes that you have some knowledge of how to edit and work with Apache2. If you have never worked with Apache2 then feel free to contact us for help!

Let’s get started!

  1. SSH into your server
  2. We need to turn on the Apache2  ReWriteEngine. This module allows Apache2 to redirect a user to different files based on specific instructions we give it.
  3. Execute the command sudo a2enmod rewrite
  4. You will need to restart Apache2 after this command sudo systemctl restart apache2
  5. Now that we have that done let’s navigate over to where we have our website stored.
  6. Locate or create the file .htaccess
  7. Let’s add some code to this file. If this file exists already then you will want to verify that these lines don’t already exist.
    Options +FollowSymLinks
    RewriteEngine on

    These two lines allow Apache2 to first, follow SymLinks and second allows the website to utilize the ReWrite Engine which we enabled previously.
  8. Now we can redirect our old not secure URL to our new SSL enabled URL.
  9. Inside the same .htaccess file add the following lines of code. Note: they need to be underneath the rules we added earlier.
    RewriteCond %{HTTPS} off [OR] RewriteCond %{HTTP_HOST} ^www\. [NC] RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L,NE]
    Replace https://example.com with your websites domain name.

So what did we do here? Pretty simple. We make Apache2 do all the hard work for us. We turned on the ReWrite Module and then told Apache2 that every incoming server request that wasn’t secure to redirect to the same URL only in a secure fashion. We did this all without losing any SEO rankings!

    Share This Guide, Choose Your Platform!

    Connect with 45Squared

    Exit mobile version