0

我有一个要求,我必须从远程主机名中提取值并将其设置在标头中,例如如果 URL 是这样的 -
http://data1-data2.server.com/someuri

从上面的例子中,data1和data2需要被提取并设置在一个header中,即X-HEADER1=data1 & X-HEADER2=data2。

我最初尝试了 RewriteRule 但发现它仅适用于 URI 而不是 servername

RewriteRule (?<=//)(.*)(?=-) - [E=INFO_REQUEST_CONTEXT_1:$1,NE]
RewriteRule (?<=-)(.*?)(?=[.]) - [E=INFO_REQUEST_CONTEXT_2:$1,NE]
RequestHeader set X-HEADER1 "%{INFO_REQUEST_CONTEXT_1}e"
RequestHeader set X-HEADER2 "%{INFO_REQUEST_CONTEXT_2}e"

什么是正确的配置?

4

2 回答 2

1

以下配置工作,

# Extract data1 from URL and set the header
SetEnvIf Host "(.*)(?=-)" var1=$1   
RequestHeader set X-HEADER1 %{var1}e env=var1
# Extract data2 from URL and set the header 
SetEnvIf Host "(?<=-)(.*?)(?=[.])" var2=$1  
RequestHeader set X-HEADER2 %{var2}e env=var2
于 2013-11-05T17:59:32.260 回答
0

启用mod_rewritemod_headers通过httpd.conf(如果尚未启用),然后将此代码放入您的DOCUMENT_ROOT/.htaccess文件中:

RewriteEngine On

# capture REMOTE_HOST components in env variables
RewriteCond %{REMOTE_HOST} ^([^-]+)\.([^.]+)\.
RewriteRule ^ - [E=INFO_REQUEST_CONTEXT_1:%1,E=INFO_REQUEST_CONTEXT_2:%2,L]

## set custom response headers to be used in server side scripts
RequestHeader set X-HEADER1 "%{INFO_REQUEST_CONTEXT_1}e"
RequestHeader set X-HEADER2 "%{INFO_REQUEST_CONTEXT_2}e"

## set custom response headers    
Header always set X-HEADER1 "%{INFO_REQUEST_CONTEXT_1}e"
Header always set X-HEADER2 "%{INFO_REQUEST_CONTEXT_2}e"

mod_headers 参考

于 2013-10-28T10:20:18.910 回答