How to combine two Apache htaccess redirect conditions (http to https and non-www to www)
.htaccess, apache, mod-rewrite, redirect, regex
Solution
You can combine both rules into one using `OR` condition clause:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.domain.com%{REQUEST_URI} [R=301,L,NE]
Just replace `www.domain.com` with your actual domain name. Also it is better to test this in a new browser to avoid 301 caching issues.
Problem
I have two redirect conditions which work independently fine but I want to avoid the case where both conditions are satisfied (which results in two redirect steps instead of one) I must confess I am not good with Apache regex so I got them off the net. Any idea how to combine the two (A or B kinda logic with regex)? Here is the code: ``` RewriteEngine On # redirect from http to https RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} # redirect from non-www to www https RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L] ``` Output of the multiple redirect result when both conditions are satisfied is shown below: