1

I need a simple solution for chaining Conditions but I end always with errors:

If its not the remote address 123.123.123.123 AND its http host ex.example.com GOTO example.example.com

I came up with this (which doesn't work):

RewriteCond %{REMOTE_ADDR} !^123\.123\.123\.123$ [C]
RewriteCond %{HTTP_HOST} ^ex\.example\.com$ [NC]
RewriteRule . http://example.example.com%{REQUEST_URI} [R,L]

I thought the [C] flag can manage this but it doesn't. I didn't found any examples on this

1 Answer 1

2

If by chaining you mean that a RewriteCond depends on a previous one, you don't need [C].

They're already connected by an implicit AND. So the following would suffice:

RewriteCond %{REMOTE_ADDR} !=123.123.123.123
RewriteCond %{SERVER_NAME} =ex.example.com [NC]
RewriteRule .* http://example.example.com%{REQUEST_URI} [R=301,L]

Note the use of %{SERVER_NAME} instead of %{HTTP_HOST}.

If your default virtual host accepts all incoming requests, it is not safe to rely on HTTP_HOST, since its value is taken from the HTTP header field Host: which can be forged!

3
  • Thanks, this works. If I need another IP Adresse should I just add another line between first and second and add an [OR] Flag after the first one?
    – Xaver
    Jan 11, 2011 at 9:05
  • 1
    Note: RewriteCond %{REQUEST_URI} (.*) is completely redundant. Just use %{REQUEST_URI} instead of %1.
    – Denis Howe
    Sep 28, 2016 at 12:16
  • Indeed, Denis. Why don't you go ahead and edit my answer. Sep 28, 2016 at 12:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.