针对不同场景的RewriteEngine

问题描述 投票:0回答:1

我在 Linux 机器上使用 RewriteEngine 有点困难。

需要有 4 个场景,2 个工作正常,2 个不能(当然,因为我做了任何类型的配置错误)。

我的目标:

  1. 有效

仅对 192.168.1.10x IP 有效
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. 有效

仅对 192.168.1.15x IP 有效
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. 不起作用

仅对 192.168.1.10x IP 有效
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

转到 http://192.168.1.2:80/my/fancy/folder/myFile.json 然后我被重定向到 http://192.168.1.3:80 但不 http://192.168.1.3:80/my/fancy/folder/myFile.json


  1. 不起作用

仅对 192.168.1.15x IP 有效
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

转到 http://192.168.1.2:80/my/fancy/folder/myFile.json 然后我被重定向到 http://192.168.1.3:80 但不 http://192.168.1.3:80/my/fancy/folder/myFile.json

mod-rewrite apache2
1个回答
0
投票
  1. 不起作用

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

(同样适用于#3和#4。)

RewriteRule
pattern
^
对于每个 URL 路径都是成功的,但您只是重定向到
http://192.168.1.3:80
- 您需要附加原始 URL 路径。

从您的示例中不清楚您是否只想定位

/my/fancy/folder/myFile.json
,或者是否旨在对任何 URL进行“一般性发言”?

如果只是一个静态 URL,则修改

RewriteRule
,如下所示:

:
RewriteRule ^my/fancy/folder/myFile\.json$ http://192.168.1.3:80/$0 [R=302,L]

其中

$0
反向引用包含
RewriteRule
pattern 的整个匹配(只需在
substitution
字符串中保存重复的 my/fancy/folder/myFile.json)。

但是,如果这适用于 any URL,则相应地修改

RewriteRule
。例如:

:
RewriteRule .* http://192.168.1.3:80/$0 [R=302,L]

请注意,您还缺少前面规则中的

R
L
标志。如果没有它,这个“默认”为 302(临时)重定向。您需要 L 标志来防止后续指令被(不必要地)处理。
    

© www.soinside.com 2019 - 2024. All rights reserved.