First you need to install SSL Certificate Successfully. Once your done that choose any of example to do redirect url.
Example : 1
In this example you don't need mod rewrite module to be enabled.
Open your apache configuration file
Example : 1
In this example you don't need mod rewrite module to be enabled.
Open your apache configuration file
#nano /etc/apache2/sites-enabled/000-default.conf
Modify your VirtualHost Tag as followed
<VirtualHost *:80>
ServerName www.example.com (your domain name)
Redirect / https://www.example.com/
</VirtualHost>
Save and exit. Restart apache service once
#service apache2 restart
Check your domain url with http it will redirect automatically on HTTPS.
Example : 2
In this Example you have to enable mod rewrite module first with below command
#a2enmod rewrite
Now, Copy below code in your .htaccess file without any changes on beginning of .htaccess file.
# BEGIN REWRITE SITEWIDE HTTP TO HTTPS
# This will enable the Rewrite capabilities
RewriteEngine On
# This checks to make sure the connection is not already HTTPS
RewriteCond %{HTTPS} !=on
# This rule will redirect users from their original location, to the same location but using HTTPS.
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
# END REWRITE SITEWIDE HTTP TO HTTPS
Save and exit from file.
Now open your apache configuration file and make mentioned changes in your file.
1) "AccessFileName .htaccess" tag should be open.
2) Check Directory tag and change "AllowOverride None" to "AllowOverride All"
e.g.
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
Save and exit the file.
Restart Apache service once
#service apache2 restart
#service apache2 restart
Check your domain url with http it will redirect automatically on HTTPS.
Example : 2
In this Example you have to enable mod rewrite module first with below command
#a2enmod rewrite
Now, Copy below code in your .htaccess file without any changes on beginning of .htaccess file.
# BEGIN REWRITE SITEWIDE HTTP TO HTTPS
# This will enable the Rewrite capabilities
RewriteEngine On
# This checks to make sure the connection is not already HTTPS
RewriteCond %{HTTPS} !=on
# This rule will redirect users from their original location, to the same location but using HTTPS.
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
# END REWRITE SITEWIDE HTTP TO HTTPS
Save and exit from file.
Now open your apache configuration file and make mentioned changes in your file.
1) "AccessFileName .htaccess" tag should be open.
2) Check Directory tag and change "AllowOverride None" to "AllowOverride All"
e.g.
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
Save and exit the file.
Restart Apache service once
#service apache2 restart
All Done Check now !!!