RewriteEngine for different scenarios

32 Views Asked by At

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:

  1. 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

  1. 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

  1. 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


  1. 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

1

There are 1 best solutions below

0
MrWhite On
  1. Does not work

    RewriteCond %{SERVER_PORT} =80
    RewriteCond %{REMOTE_ADDR} ^192\.168\.1\.10[0-9]$
    RewriteRule ^ http://192.168.1.3:80
    

(The same applies to both #3 and #4.)

The RewriteRule pattern ^ is successful for every URL-path, but you are only redirecting to http://192.168.1.3:80 - you need to append the original URL-path.

Aside: Note that you are also missing the R and L flags from all the rules. Without which this defaults to a 302 (temporary) redirect, but you should be explicit. You need the L flag 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.json or whether that is intended to be "general speak" for any URL?

If it's just that one static URL then modify the RewriteRule like so:

:
RewriteRule ^/?my/fancy/folder/myFile\.json$ http://192.168.1.3:80%{REQUEST_URI} [R=302,L]

The REQUEST_URI server 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.json in the substitution string.

If, however, this is intended to work for any URL then modify the RewriteRule accordingly. For example:

:
RewriteRule ^ http://192.168.1.3:80%{REQUEST_URI} [R=302,L]

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 /api only (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 /api then you need to specify this URL-path in the rule. For example:

RewriteRule ^/?api$ http://192.168.1.3:8080%{REQUEST_URI} [R=302,L]