WebFaction
Community site: login faq

I have three domains - domain.com, domain.co.uk and domain.ru. I'm trying to achieve the following redirects in one .htaccess file:

  • all requests should be redirected to include the prefix www.
  • all requests to (www.)domain.com should be redirected to www.domain.co.uk
  • all requests on http should be redirected to use https
I figured it would be best to avoid having individual applications redirecting a variety of scenarios, to keep the number of redirects down. Using the WebFaction documentation on Redirecting a Domain with a Static/CGI/PHP App and Redirecting from HTTP to HTTPS I mashed together the following:

Options +FollowSymLinks
RewriteEngine on
#1
RewriteCond %{HTTP:X-Forwarded-SSL} !on
#2
RewriteCond %{HTTP_HOST} (www\.)?domain\.(com|co.uk)$ [NC]
RewriteRule ^(.*)$ https://www.domain.co.uk/$1 [R=301,L]
#3
RewriteCond %{HTTP_HOST} (www\.)?domain.ru$ [NC]
RewriteRule ^(.*)$ https://www.domain.ru/$1 [R=301,L]

It seems to work OK, but I'm having a hard time understanding the logic flow and wondering about the use of condition #1 - RewriteCond %{HTTP:X-Forwarded-SSL} !on .

What happens if condition #1 is false? Would the logic exit there, or would condition #3 then be evaulated? Or is condition #3 only ever evaluated if condition #1 is true and condition #2 is false?

Either way, since the only domains pointing to this application are http, condition #1 should always evaluate to true - or do I misunderstand?

asked 18 Mar '15, 11:14

tomtom
312
accept rate: 0%


Consecutive RewriteCond directives are considered to part of the same test, so as written above, the logic is like this:

# if these two conditions are true...
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteCond %{HTTP_HOST} (www\.)?domain\.(com|co.uk)$ [NC]
# then do this:
RewriteRule ^(.*)$ https://www.domain.co.uk/$1 [R=301,L]

# otherwise if this condition is true...
RewriteCond %{HTTP_HOST} (www\.)?domain.ru$ [NC]
# then do this:
RewriteRule ^(.*)$ https://www.domain.ru/$1 [R=301,L]

In other words, your SSL test is never going to be applied for domain.ru (#3 in your example) because it's only evaluated on the first set of conditions. If you want that one to be tested for SSL as well, then you'll need to specify that condition again, eg:

RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteCond %{HTTP_HOST} (www\.)?domain\.(com|co.uk)$ [NC]
RewriteRule ^(.*)$ https://www.domain.co.uk/$1 [R=301,L]

RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteCond %{HTTP_HOST} (www\.)?domain.ru$ [NC]
RewriteRule ^(.*)$ https://www.domain.ru/$1 [R=301,L]

That said, if this app is only being served on HTTP, then there's really no need to include the SSL test. In fact, you might even be able to condense this into a single condition and rule that uses the regex matches from the condition to determine the domain to which to rewrite the request, maybe something like this:

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

Hope that helps!

permanent link

answered 18 Mar '15, 18:41

seanf
12.2k21736
accept rate: 37%

edited 18 Mar '15, 18:43

Thank you, seanf! Just what I needed.

(20 Mar '15, 15:54) tomtom
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×72
×33
×13

question asked: 18 Mar '15, 11:14

question was seen: 2,678 times

last updated: 20 Mar '15, 15:54

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2016 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM