Apache 2.4剥离查询参数并将其添加为标题

时间:2018-08-01 20:11:51

标签: apache mod-rewrite apache2.4

我有以下网址-

http://ip:8080/dashboard/db/testdash?id=add&t1=abc

我想从网址中剥离t1并将其作为标头添加到我的代理请求中。我正在使用反向代理conf,我想截取该网址,删除t1参数,并将其添加为标头,然后将其发送到目标

对于在8080上侦听的虚拟主机,我具有以下配置

RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)t1=(.*)
>>>Need to add the rewrite logic here
>>>Add rewrite conf here change url to /dashboard/db/testdash?id=add

//Add the request header 
>>>RequestHeader set X-T1 "value of the query parameter"

//Proxy 
ProxyRequests Off
ProxyPass "/" http://localhost:3000/
ProxyPassReverse "/" http://localhost:3000/

Header always set Access-Control-Allow-Origin "*"
Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT"
Header always set Access-Control-Max-Age "1000"
Header always set Access-Control-Allow-Headers "*"

# Added a rewrite to respond with a 200 SUCCESS on every OPTIONS request.
RewriteEngine On
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule ^(.*)$ $1 [R=200,L]

我对httpd完全陌生,所以不要怎么做!

1 个答案:

答案 0 :(得分:0)

  • 删除ProxyPass,因为我们要使用变量
  • 添加

    RewriteRule(^ /。*)http://localhost:3000/ $ 1?%1

    请注意,如果URL末尾可以有更多键,则此匹配是不够的!一个更强大的正则表达式只能通过捕获多达&来吞噬t1值。例如^(。)t1 =([[^&]。)(。*)然后替换%1%3

  • 添加一个古怪的空白部分,以弥补已删除的Proxypass

    <代理http://localhost:3000/>