How To Chain on mod_rewrite
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 does开发者_运维百科n't. I didn't found any examples on this
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!
精彩评论