I'm a bit struggling with the RewriteEngine using with a Linux machine.
Needed is to have 4 scenarios, 2 are working fine, 2 not (for sure because I do any kind of misconfiguration).
My goals:
- Works
Only valid for 192.168.1.10x IPs
http://192.168.1.2:81/api?key=value -> http://192.168.1.3:8080/api?key=value
RewriteCond %{SERVER_PORT} =81
RewriteCond %{REMOTE_ADDR} ^192\.168\.1\.10[0-9]$
RewriteRule ^ http://192.168.1.3:8080/api
- Works
Only valid for 192.168.1.15x IPs
http://192.168.1.2:81/api?key=value -> http://192.168.1.4:8080/api?key=value
RewriteCond %{SERVER_PORT} =81
RewriteCond %{REMOTE_ADDR} ^192\.168\.1\.15[0-9]$
RewriteRule ^ http://192.168.1.4:8080/api
- Does not work
Only valid for 192.168.1.10x IPs
http://192.168.1.2:80/my/fancy/folder/myFile.json -> http://192.168.1.3:80/my/fancy/folder/myFile.json
RewriteCond %{SERVER_PORT} =80
RewriteCond %{REMOTE_ADDR} ^192\.168\.1\.10[0-9]$
RewriteRule ^ http://192.168.1.3:80
Going to http://192.168.1.2:80/my/fancy/folder/myFile.json then I get redirected to
http://192.168.1.3:80 but not to
http://192.168.1.3:80/my/fancy/folder/myFile.json
- Does not work
Only valid for 192.168.1.15x IPs
http://192.168.1.2:80/my/fancy/folder/myFile.json -> http://192.168.1.4:80/my/fancy/folder/myFile.json
RewriteCond %{SERVER_PORT} =80
RewriteCond %{REMOTE_ADDR} ^192\.168\.1\.15[0-9]$
RewriteRule ^ http://192.168.1.4:80
Going to http://192.168.1.2:80/my/fancy/folder/myFile.json then I get redirected to
http://192.168.1.3:80 but not to
http://192.168.1.3:80/my/fancy/folder/myFile.json
(The same applies to both #3 and #4.)
The
RewriteRulepattern^is successful for every URL-path, but you are only redirecting tohttp://192.168.1.3:80- you need to append the original URL-path.Aside: Note that you are also missing the
RandLflags from all the rules. Without which this defaults to a 302 (temporary) redirect, but you should be explicit. You need theLflag to prevent the directives that follow from being (unnecessarily) processed.It's not clear from your example whether you only want to target
/my/fancy/folder/myFile.jsonor whether that is intended to be "general speak" for any URL?If it's just that one static URL then modify the
RewriteRulelike so:The
REQUEST_URIserver variable (the value of which is accessed using the syntax%{REQUEST_URI}) contains the requested URL-path (including the slash prefix). This saves repeating/hardcoding the URL-path/my/fancy/folder/myFile.jsonin the substitution string.If, however, this is intended to work for any URL then modify the
RewriteRuleaccordingly. For example:Rules #1 and #2 that "work"
The reason why #1 and #2 "work" is because the query string part of the URL (ie.
key=value) - which is not part of the URL-path - is passed through to the substitution string by default.However, these two rules are not specifically targeting
/apionly (as the description seems to imply), they redirect/<anything>to/api(a many-to-one redirect) - which is probably not the intention. (?)If you only want to target
/apithen you need to specify this URL-path in the rule. For example: