PDA

View Full Version : Redirect HTTP:// and HTTP://WWW to HTTPS://



Fli
03-04-2017, 04:53 PM
Hello,

i wanted to prevent duplicity when users can access https://www http://www http:// https://
i wanted to use only one and redirect 301 others because Google told me (in Webmaster tools) they will display some insecure warning to the Chrome visitors if they visit http:// page equiped by login form.

WANTED RESULT
https://domain.tld

UNWANTED RESULT
http://domain.tld
http://www.domain.tld

And this worked good. Just add this into your .htaccess file located in website root directory:


# Redirect HTTP:// and HTTP://WWW to HTTPS://
# www to non-www HTTPS
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# non-www to HTTPS
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

You can then verify redirect here http://redirectcheck.com/index.php
If you can improve this, please kindly add reply.

NOTE 1: some Ad/link selling systems may fail to find ad on your page if there is a redirect. if you show ads based on condition, like full URL and you hardcoded the http:// URL, condition won't be met, so change hardcoded http into https maybe.
NOTE 2: search your site for occurrences of:
src="http:
src=http:
and replace by https as it may warn website visitor that some parts of web page are insecure.
Linux command to find that:
grep -RlE "src=http:|src=\"http:" /home/username/public_html