Redirection using SSL Certificates without errors

steven
Posts: 133
Joined: Sun Oct 01, 2017 3:08 pm

Redirection using SSL Certificates without errors

Post by steven »

Changing your domain name when using SSL can result in lowering SEO rankings and losing links. When someone visits, using your old URL, they may receive a browser error. The error is usually accompanied by various messages including warnings that the site is not secure, incorrect credentials were presented, the owner has configured their website improperly or the site is not available and try again at a later time. Unfortunately browsers may not connect even if you modified your htaccess file, depending on the user's security settings.

This occurs because browsers look for the correct security credentials first to avoid improper redirects. Rewrites in an htaccess file do not solve the problem. You should first allow browsers to connect securely then use rewrites to redirect to the new domain.

One way to correct this issue is to use a Virtual Host. Virtual Hosts allow one IP address to host multiple domains. This is easy if you are using a Synology server. Fortunately hosting providers usually provide a way to run Virtual hosts as well. When you configure the virtual host, use the domain name-based option. Enter your old domain name using the default port settings which is 80 for HTTP and 443 for HTTPS. In the document root window, enter the path to the root of your new website. You may be required to select a backend server (nginx, Apache, etc) and the php version you want to use. Make your selections, then save the virtual host and exit. Two virtual hosts may be required if you had both WWW and non-WWW websites. Just create two virtual hosts, one with WWW and one without.

Open the server or host control console and locate the security or SSL certificate options. The new SSL certificate should be the default certificate for the new domain name. Hopefully you did not delete the old SSL certificate for your prior domain. If you deleted the old certificate, install it again. Then select the old certificate and configure it for the virtual host(s) you just created. If the old certificate has expired, it must be renewed if you want to allow connections to use your old domain name without errors.

Once you've completed the above instructions, redirects in your htaccess file should work correctly. There are many examples of redirect codes online that perform various functions. Some sample codes are shown below. If you copy and paste, remember to replace the example names with your domain names.

Code: Select all

RewriteEngine On
RewriteBase /
## check if user connected using the old domain name
RewriteCond %{HTTP_HOST} ^old_domain_name$
## they did so redirect to the new domain name
RewriteRule ^(.*)$ https://new_domain_name/$1 [R=301,L]
If you have a SSL certificate installed and want to use HTTPS for all pages, the code below checks if the current connection is HTTPS and forces an HTTPS connection if it is not. This code can be placed after the code above. The R=301 option lets browsers know your redirect is permanent.

Code: Select all

RewriteEngine On
## check to see if the current connection is HTTPS
RewriteCond %{HTTPS} !=on
## it is not so force a HTTPS connection
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
Do not force all connections to use HTTPS if you have some pages that are served HTTP because that will cause an error on those pages.

Note: these instructions apply to domain name changes on the same host or server.


Last bumped by steven on Mon Oct 16, 2017 1:19 am.