【问题标题】:No SCRIPT_URI or SCRIPT_URL in my phpinfo我的 phpinfo 中没有 SCRIPT_URI 或 SCRIPT_URL
【发布时间】:2013-07-16 14:28:12
【问题描述】:

我正在运行带有 Apache 2.0.50 和 PHP 5.2.11 的 WAMP 2.2,我查看了我的 phpinfo,但我找不到 SCRIPT_URI or SCRIPT_URL 我尝试将此脚本添加到我的 httpd.conf 文件中,但它没有工作.有人对SCRIPT_URI or SCRIPT_URL 我的phpinfo 有任何想法吗?我在本地机器上运行的站点需要它。

<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>

我必须在我的 php.ini 文件中做些什么才能让它工作吗?

【问题讨论】:

  • Apache 2.0.50

标签: php apache wamp


【解决方案1】:

解决方案优先:我相信这是.htaccess 与虚拟主机的区别。如果你在.htaccess 中使用RewriteRule,你会得到REDIRECT_URL 设置,而如果你在主apache 配置的虚拟主机中使用它们,你会得到SCRIPT_URL 设置。我的解决方法非常务实:

if(array_key_exists('SCRIPT_URL',$_SERVER)){
  $url = $_SERVER['SCRIPT_URL'];
  }
elseif(array_key_exists('REDIRECT_URL',$_SERVER)){
  $url = $_SERVER['REDIRECT_URL'];
  }
else throw...;

背景说明:

我刚刚并排比较了两台服务器,一台(Mint 16,即 Ubuntu 13.10),其中REDIRECT_URL 可用(而SCRIPT_URL 不是),另一台(Ubuntu 14.04) 其中没有设置任何REDIRECT_* 变量,但SCRIPT_URL 可用相同的信息。

它们是 Apache 2.4.6 与 2.4.7,以及 PHP 5.5.3-1ubuntu2.6 与 5.5.9-1ubuntu4.3。我刚刚并排比较了phpinfo() 输出,除了库中的小版本更改之外,它们基本上是相同的,除了对于 REDIRECT 与 SCRIPT $SERVER 变量差异! (要彻底:Mint 16 机器在 Ubuntu 14 没有安装的地方安装了 mcrypt,而 Ubuntu 14 在 Mint 16 机器没有安装的地方安装了 readline。)

最大的区别是设置SCRIPT_URL的服务器在虚拟主机中具有RedirectMatch,并且需要在脚本名称前显式正斜杠:

RewriteRule ^.*$ /main.php [NC,L]

而我所有其他设置REDIRECT_URL 的服务器在.htaccess 文件中都有规则,并且不需要那个正斜杠:

RewriteRule ^.*$ main.php [NC,L]

(在这两种情况下我都没有明确设置 RewriteBase,但the manual entry 似乎暗示了我不需要它的原因,这也向我暗示该机制可能足够不同以至于设置了不同的环境变量.)

【讨论】:

  • SCRIPT_URLSCRIPT_URL 在 Apache 文档中记录为 environment variables maintained by mod_rewrite - 因此它们应该出现在 $_SERVER 超全局中。但是,由于某种原因,它们并不总是被设置。我有 3 台服务器;它们只设置在其中一个上。
【解决方案2】:

在上面的脚本中添加了这一行,现在它可以工作了RewriteEngine On

【讨论】:

    猜你喜欢
    • 2011-04-17
    • 1970-01-01
    • 2016-08-02
    • 2011-04-14
    • 2012-01-13
    • 2015-10-31
    • 2018-09-28
    • 2011-07-27
    • 2018-03-03
    相关资源
    最近更新 更多