Help me understand .htaccess Rewrite syntax please!

Hi,

The scenario is that I have an SSL certificate for my domain and I want to redirect users to https://www.mydomain.com regardless of whether they include http or www in the url.

I got it working after some Googling and trial and error but I’d appreciate it if anyone could explain to me why this works:

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

RewriteCond %{SERVER_PORT} !^443
RewriteRule ^(.*)$ https://www.mydomain.co.uk/$1 [R=301,L]

But this doesn’t:

RewriteCond %{HTTP_HOST} !^www\\.mydomain\\.com [NC]
RewriteCond %{SERVER_PORT} !^443
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^www\\.mydomain\\.com [NC]
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]

“If the requested domain is not www.mydomain.com, send the request to https://www.mydomain.com

RewriteCond %{SERVER_PORT} !^443
RewriteRule ^(.*)$ https://www.mydomain.co.uk/$1 [R=301,L]

“If the request came on port 80 (insecure) then send the request to https://www.mydomain.co.uk

RewriteCond %{HTTP_HOST} !^www\\.mydomain\\.com [NC]
RewriteCond %{SERVER_PORT} !^443
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]

“If the requested domain is not www.mydomain.com AND the request came on port 80, send the request to https://www.mydomain.com

A request for http://www.mydomain.com does not meet the first condition, a request for https://mydomain.com does not meet the second condition, so neither of them will be redirected.

To get what you want, you need an [OR] between those conditions.

Off Topic:

Why is this in a PHP forum?

Cheers Dan

I assume it makes little difference, but would I be better off leaving it as it is (with the two separate rewrite rules), or using an OR statement?

If it’s an OR statement, how would I write that?

Apologies for posting this in the wrong place.

Billy,

RewriteCond statements (and RewriteRule statements) are ANDed by default UNLESS an OR flag is used, i.e.,

RewriteCond %{HTTP_HOST} !^www\\.mydomain\\.com [NC,OR]
RewriteCond %{SERVER_PORT} !^443
RewriteRule .? https://www.mydomain.com%{REQUEST_URI} [R=301,L]

Note that I replaced your :kaioken: EVERYTHING :kaioken: atom in the rule’s regex with the already existing {REQUEST_URI}.

Regards,

DK

Cheers for that David - as you can probably tell I’m new to this so your help is much appreciated! I’ve implemented your code successfully. I guess that using the OR flag is preferable to having the two separate rewrite rules as it means that my code is simpler and faster (although the difference is no doubt negligible) - and it also means that future changes will be easier to implement.

Thanks again

Billy

No sweat, Billy. If you need more information, you might benefit from reading the mod_rewrite tutorial linked in my signature as it contains explanations and sample code. It’s helped may members and should help you, too.

Regards,

DK