Categories
SEO Web Developing

http to https, www to non-www Redirect

When it comes to search engine indexing, we need to ensure only ONE version of either https://non-www or https://www indexed. Our preferred way is to use https:/non-www as the default website address, such as https://eharvest.com.au. This requires all other three website address variants to be redirected at the server level to your default URL.

http://eharvest.com.au -> https://eharvest.com.au
http://www.eharvest.com.au -> https://eharvest.com.au
https://www.eharvest.com.au -> https://eharvest.com.au

If you’re using a CMS like WordPress and happened to set www version as your default site URL, simply removing the www in your URL configuration will likely to cause your site entering a redirect loop. To resolve this, follow below two steps:

1. Do a database search and replace to remove the www in all the appearance of your database tables. See code and steps below:

Do a database dump: 
mysqldump -u user -p databasename > database.sql

Run sed command to find/replace to remove the www. string
sed -i 's/www.domain.com/domain.com/g' database.sql

Re-import the updated database
mysql -u user -p databasename < database.sql

2. Redirect www to non-www and http to https which can be done in the .htaccess file, as below:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

By Ethan

To many, a business is a lifetime commitment. It's easy to start one yet difficult to make it successful. Attitude, skills, experiences and dedication help hone the craft along the way, but it's often the great vision and resilience to remain focused wins the game. Read more about me here