我有一个要求,我必须从远程主机名中提取值并在标题中设置它,例如,如果URL是这样的 -
http://data1-data2.server.com/someuri
从上面的例子中,需要提取data1和data2并将其设置在标题中,即X-HEADER1 = data1& X-HEADER2 = DATA2。
我最初尝试过RewriteRule,但发现它只适用于URI而不适用于服务器名称
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"
使用什么是正确的配置?
答案 0 :(得分: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
答案 1 :(得分:0)
启用mod_rewrite
和mod_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"