Looking to Build/Redesign a Website or Blog ?

To speak to Rakshit, call +91 968 704 2303

[.htaccess] Redirect Non WWW to WWW with SSL/HTTPS

Hello Friends,

I was working with one project where I implement facebook login application. I was looking site with www url and client was checking with non www url. All works fine for me and never worked for client. It was a irritating couple of hours time for me and client both when we were discussing.

I noticed and came to know that Facebook login works with www in url as i mention path in facebook app with www. Its easy to redirect your site url from non www to www using htaccess. But here the problem is we have ssl installed and whole site is working under ssl certificate.

I already put code for redirect http to https urls using htaccess. Now I need to make both works at a time. One for http to https and other is non www to www. It was adding 2 www in url and not redirect to https, ignore when we have www in url and so on. After few time of work on rules, Here is the rules for both working.

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Rewrite rule for non www to www with ssl, non www to www with https, non www to www with http to https, redirect non www to www with ssl

Share:

1 Comment

Leave a Reply

Your email address will not be published.