htaccess: Redirecting different domains with different conditions

When developing large applications you face some issues or get some requirements, that you have to fulfill for your clients. Same as one of our white label client demanded non SSL website. Where our production environment is running on HTTPS and we are redirecting non SSL to SSL forcefully with .htaccess As below.

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

But as per requirement only one white label domain should run on HTTP only. This can be achieved by 2 methods.
Either we do conditional changes in our product OR we do changes in htaccess.
I choose second one.

Redirecting Multiple domains to same site only with htaccess

Following lines are to redirect specific domain (white label client’s domain) to HTTP only, and our production domain to HTTPS

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^whitelabelclientsdomain\.com$ [NC]
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R,L]

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} mainproductionurl\.com [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

Redirecting WWW and non-WWW

Additionally, If you want to redirect www and non www domains try following code.

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

Please note that all the above code will work in your htaccess only if you have mod_rewrite module enabled in your apache.

Replace your domains accordingly.
Enjoy!!!

Shyam Makwana has written 19 articles

I am PHP developer with passion of exploring IT world for new technologies. I do projects in WordPress, Joomla, Magento, Drupal, Open Cart, Code Igniter, MVC and CorePHP. Checkout my about.me profile here http://about.me/shyammakwana to know more about me.