我怎样写SCRIPT_URI在Apache的一个标题? [英] How do I write SCRIPT_URI to a header in Apache?

查看:137
本文介绍了我怎样写SCRIPT_URI在Apache的一个标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个设置,其中我有这样的服务器:

I have a setup where I have servers like this:

负载平衡器 - >阿帕奇 - > Tomcat的

load balancer -> Apache -> Tomcat

我想Apache来编写客户端使用到标题中的URL,这样我就可以阅读,一旦我打的tomcat。

I would like Apache to write the url that the client used into a header, so I can read that once I hit tomcat.

我试着使用mod_rewrite和mod_headers中这样做,但没有运气。
如果我看 http://httpd.apache.org/docs/2.0/mod /mod_rewrite.html 那么它似乎很清楚,我需要一个名为SCRIPT_URI变量:

I've tried to use mod_rewrite and mod_headers so do it, but with no luck. if I look at http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html then it seems clear that i need the variable called SCRIPT_URI:

SCRIPT_URI=http://en1.engelschall.com/u/rse/

我也看了这个 http://www.askapache.com /htaccess/crazy-advanced-mod_rewrite-tutorial.html ,以便弄清楚如何写头和小有更迭,但还不够。

I also looked at this http://www.askapache.com/htaccess/crazy-advanced-mod_rewrite-tutorial.html so figure out how to write headers and have had some succes, but not enough.

我已经安装了Apache服务器上的PHP,如果我看的phpinfo()我可以看到SCRIPT_URI是存在的,有一个合理的值。

I have php installed on the apache server, and if i look at phpinfo() i can see the SCRIPT_URI is there and has a sensible value.

我只是不能让它将其写入到一个头。下面是我做了什么简化版本:

I just can't get it to write it to a header. Here's a simplified version of what I've done:

#load modules
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule headers_module modules/mod_headers.so
#Get the original uri used
RewriteRule .* - [E=INFO_SCRIPT_URI:%{SCRIPT_URI},NE]
RequestHeader set x-orig-uri "%{INFO_SCRIPT_URI}e"

我试过其他几个选项和两个窗户上,Cygwin和Ubuntu Linux操作系统

I've tried several other options and both on windows, cygwin and ubuntu linux

任何想法?

推荐答案

我找到了自己的解决方法,但它不是干净,简单的解决方案,我想。
我能得到的indvidual部分,可以使用它们来构建完整的URI:

I found a workaround myself, although it's not the clean and easy solution I wanted. I was able to get the indvidual parts and can use them to build the full URI:

<IfModule !rewrite_module>
    LoadModule rewrite_module modules/mod_rewrite.so
</IfModule>

<IfModule !headers_module>
    LoadModule headers_module modules/mod_headers.so
</IfModule>


<IfModule rewrite_module>
<IfModule headers_module>

####### INITIAL SETUP #########################
    RewriteEngine on

####### SET HEADERS #########################
    #get and set the host name
        RewriteRule .* - [E=INFO_HTTP_HOST:%{HTTP_HOST},NE]
        RequestHeader set x-orig-host "%{INFO_HTTP_HOST}e"


    #get and set the host port
        RewriteRule .* - [E=INFO_SERVER_PORT:%{SERVER_PORT},NE]
        RequestHeader set x-orig-port "%{INFO_SERVER_PORT}e"


    #If the uri starts with a slash and some alphanumerics, then make a 
    #group of that until the first non-alpha (ie. the next slash)
        RewriteCond %{REQUEST_URI} ^(/[\w-]+)
    #Save the content of the regex match group ( %1 ) in an environment variable
        RewriteRule .* - [E=INFO_REQUEST_CONTEXT:%1,NE]
    #Set a header with the content of the environment variable
        RequestHeader set x-orig-context "%{INFO_REQUEST_CONTEXT}e"


    #If the accept-header contains a number after ;version= then make a regex group of that number
        RewriteCond %{HTTP_ACCEPT} \+json;version=(\d+)$ 
    #Save the content of the regex match group ( %1 ) in an environment variable
        RewriteRule .* - [E=INFO_ACCEPT_VERSION:%1,NE]
    #Set a header with the content of the environment variable
        RequestHeader set x-orig-accept-version "%{INFO_ACCEPT_VERSION}e"


    #If the accept-header contains kasia2. followed by some letters, 
    #then make a regex group of those letters
        RewriteCond %{HTTP_ACCEPT} kasia2.(\w+).*
    #Save the content of the regex match group ( %1 ) in an environment variable
        RewriteRule .* - [E=INFO_ACCEPT_NAME:%1,NE]
    #Set a header with the content of the environment variable
        RequestHeader set x-orig-accept-name "%{INFO_ACCEPT_NAME}e"


    #If https is on ...
        RewriteCond %{HTTPS} on
    #...then set the protocol environment variable to "https"
        RewriteRule .* - [E=INFO_PROTOCOL:https,NE]
    #If https is off ...
        RewriteCond %{HTTPS} off
    #...then we assume it must be "http"
        RewriteRule .* - [E=INFO_PROTOCOL:http,NE]
    #Finally, set the protocol header
        RequestHeader set x-orig-protocol "%{INFO_PROTOCOL}e"


    #Get the request uri and set an environment variable
        RewriteRule .* - [E=INFO_REQUEST_URI:%{REQUEST_URI},NE]
    #Build the whole original url out of the available parts. SCRIPT_URI is always null, otherwise we could have used that.
        RequestHeader set x-orig-url "%{INFO_PROTOCOL}e://%{INFO_HTTP_HOST}e%{INFO_REQUEST_URI}e"
    #In addition make an url with only the host and context, for convenience
        RequestHeader set x-orig-url-base "%{INFO_PROTOCOL}e://%{INFO_HTTP_HOST}e%{INFO_REQUEST_CONTEXT}e"

</IfModule>
</IfModule>

这篇关于我怎样写SCRIPT_URI在Apache的一个标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆