Loading...
BloggingTech

Force https Redirection in Bitnami WordPress URL

HTTPS Redirection

In the last article, we learned how to set up a let’s encrypt certificate in Bitnami WordPress AMI. Check it out here, if you have missed that.

Once we complete setting up Let’s Encrypt, there occurs another challenge which is to redirect users automatically to secure https URL. So let suppose a user is directly hitting http://domain.extension or http://www.domain.extension then URL shall be redirected to https://domain.extension or https://www.domain.extension automatically.

Below are the steps which you need to follow to achieve this.

Step 1: Download WinSCP. Click on the link given.

  • From the Start menu, choose All ProgramsWinSCP.
  • Once you open WinSCP, click on ‘New Site’ located at the left.
  • Then fill in the details of your site like below.
  • In Host name, fill the elastic IP of your AWS instance.
  • In User name, fill ‘bitnami’.
  • Port number shall be 22.
  • Leave Password blank.
  • Go to Advanced and you will see a screen like below.
  • Click on ‘Authentication’ located at the left panel.
  • In the section ‘Authentication parameters’, locate the .ppk file from your computer and then click ‘OK’.
  • You will go back to the previous screen, click on ‘Save’ so that all the information you have filled will be saved.
  • And then click on ‘Login’.
  • You will now be inside your virtual machine directories which are responsible for your website’s appearance.

Step 2: Add the following lines in the default Apache virtual host configuration file at /opt/bitnami/apache2/conf/bitnami/bitnami.conf, inside the default VirtualHost directive, so that it looks like this:

<VirtualHost _default_:80>   
DocumentRoot "/opt/bitnami/apache2/htdocs"   
RewriteEngine On   RewriteCond %{HTTPS} !=on   
RewriteCond %{HTTP_HOST} !^(localhost|127.0.0.1)   
RewriteRule ^/(.*) https://%{SERVER_NAME}/$1 [R,L]   
... 
</VirtualHost> 

To redirect all HTTP requests to a particular domain, specify the domain name as a static value in the rewrite rule instead of using the SERVER_NAME variable. Here’s an example of redirecting all HTTP requests to https://example.com:

<VirtualHost _default_:80>   
DocumentRoot "/opt/bitnami/apache2/htdocs"   
RewriteEngine On   RewriteCond %{HTTPS} !=on   
RewriteCond %{HTTP_HOST} !^(localhost|127.0.0.1)   
RewriteRule ^/(.*) https://example.com/$1 [R,L]   
... 
</VirtualHost> 

Step 3: Specify your domain name in the /opt/bitnami/apps/wordpress/htdocs/wp-config.php file. Edit and replace the following lines as shown, remembering to replace the DOMAIN placeholder with the actual domain name you wish to use:

define('WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST'] . '/');
define('WP_HOME', 'http://' . $_SERVER['HTTP_HOST'] . '/');

with

define('WP_SITEURL', 'https://DOMAIN/');                   
define('WP_HOME', 'https://DOMAIN/');

So once we complete these steps, our website will be served at https. That means, whenever user hits http://domain.extension or http://www.domain.extension, he/she will be redirected to https://domain.extension or https://domain.extension. But still we have one last thing to fix and that is redirecting https://domain.extension to https://www.domain.extension. You may wonder, why it is required !

This is because of the fact that search engines treat https:// and https://www. as different URLs. And if we keep running two URLs for same website, they will consider this to be a substandard practice and our URL will not be promoted over search engines. So let’s get rid of this now.

How to redirect https:// to https://www.

  • Inside winSCP, Just locate the file at /opt/bitnami/apps/wordpress/conf/httpd-prefix.conf
  • Check for the below code.
RewriteEngine On
RewriteCond "%{HTTP_HOST}" ^ec2-([0-9]{1,3})-([0-9]{1,3})-([0-9]{1,3})-([0-9]{1,3})...amazonaws.com(:[0-9])?$
RewriteRule "^/?(.*)" "%{REQUEST_SCHEME}://%1.%2.%3.%4%5/$1" [L,R=302,NE]

Replace it to below code.

RewriteEngine On
RewriteCond "%{HTTP_HOST}" ^DOMAIN\.EXTENSION$
RewriteRule "^/?(.*)" "%{REQUEST_SCHEME}://www.DOMAIN.EXTENSION/$1" [L,R=302,NE]

Remember to replace DOMAIN with your actual domain name and EXTENSION with your actual extension (.tk, .com etc).

After all these changes, just restart your apache server with the help of Putty. Get into putty and run the following command.

sudo /opt/bitnami/ctlscript.sh restart

Leave a Reply

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

Skip to content