Setup Apache2 HTTP / HTTPS and Domain Redirects on Ubuntu 16.04 LTS Servers

Webmasters and students may want to learn how to redirect an old domain to a new one or one sub-domain to another or HTTP to HTTPS… For example, with Apache2 redirect modules, one can redirect http//example.net to http://example.com or http://www.example.com to http://example.com or http://example.com to https://example.com

This brief tutorial is going to show students and new users how to set up Apache2 directs easily… the method below can also be used with switching your HTTP domain to HTTPS…

When you’re ready to configure Apache2 directs, continue below:

Step 1: Install / Configure Apache2 HTTPS

To configure Apache2 to redirect all traffic to HTTPS, you must configure and enable its SSL modules.. The post below shows you how to install and configure Apache2 to communicate over HTTPS… however, both HTTP and HTTPS are enabled… the method below will show you how to redirect all traffic to the server to use HTTPS only..

Setup Apache2 HTTP with Self-Signed SSL/TLS Certificates on Ubuntu 16.04 LTS Servers

Step 2: Redirect Apache2 HTTP to HTTPS

Our post above set up Apache2 to communicate over both HTTP and HTTPS… however, in today’s environment, it’s recommended to choose one type of protocol and redirect the other to it..

For example, you should redirect all HTTP (80) to HTTPS (443). To do that, add the highlighted portion of the code to your Apache2 site configuration file…

Run the commands below to open Apache2 default site configuration file..

sudo nano /etc/apache2/sites-available/000-default.conf

Then add the highlighted portion to the bottom of the file.

Redirect to same domain on HTTPS only

<VirtualHost *:80>
     ServerAdmin admin@example.com
     DocumentRoot /var/www/html/example.com/
     ServerName example.com
     ServerAlias www.example.com

     <Directory /var/www/html/example.com/>
        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


RewriteEngine on
RewriteCond %{SERVER_NAME} =example.com [OR]
RewriteCond %{SERVER_NAME} =www.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

The configuration above redirect all requests to use HTTPS only to communicate with the server with domain name example.com. So, http://example.com and http://www.example.com will be redirect to https://example.com

Step 3: Redirect Old Domain to New

If you want to redirect from an old domain to a new one, the setting below should help you do that… For this setup, HTTPS isn’t required… Run the commands below to open Apache2 default site configuration file.

sudo nano /etc/apache2/sites-available/000-default.conf

Then add the highlighted portion to the bottom of the file.

Redirect old domain to new domain

<VirtualHost *:80>
     ServerAdmin admin@example.com
     DocumentRoot /var/www/html/example.com/
     ServerName example.com
     ServerAlias www.example.com

     <Directory /var/www/html/example.com/>
        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


RewriteEngine on
RewriteCond %{SERVER_NAME} =example.net [OR]
RewriteCond %{SERVER_NAME} =www.example.net
RewriteRule ^ http://example.com%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

The above code added to your Apache2 site configuration file will redirect an old domain to a new one without HTTPS…. the highlighted code above redirects http://example.net and http://www.example.net to http://example.com

After making the changes above, run the commands below to test your settings and enable Apache2 rewrite and SSL modules.

sudo apachectl configtest
sudo a2enmod rewrite
sudo a2enmod ssl
sudo a2ensite default-ssl

If you don’t see any error messages then you’re good. Restart Apache2 web server by running the commands below.

sudo systemctl restart apache2.service

That’s it! This is how to configure Apache2 HTTP 301 redirects

~Enjoy~

You may also like the post below:

Setup Apache2 HTTP with Self-Signed SSL/TLS Certificates on Ubuntu 16.04 LTS Servers

One Reply to “Setup Apache2 HTTP / HTTPS and Domain Redirects on Ubuntu 16.04 LTS Servers”

  1. Re: Step 2: Redirect Apache2 HTTP to HTTPS
    Hi
    Have tried the above step but it has produced the outcome. Any updates, keep me posted.
    Thanks
    Regor

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: