我的phpinfo中没有SCRIPT_URI或SCRIPT_URL

时间:2013-07-16 14:28:13

标签: php apache wamp

我正在使用Apache 2.0.50和PHP 5.2.11运行WAMP 2.2并且我查看了我的phpinfo并且我找不到SCRIPT_URI or SCRIPT_URL我尝试将此脚本添加到我的httpd.conf文件中以获取apache并且它没有'工作。任何人都有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文件中是否有必要执行此操作?

2 个答案:

答案 0 :(得分:1)

首先解决方案:我认为这是.htaccess与虚拟主机的区别。如果您在RewriteRule中使用.htaccess,则会设置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机器安装了mcrypt,而Ubuntu 14没有,而Ubuntu 14安装了readline,而Mint 16机器没有安装。)

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

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

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

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

(在任何一种情况下我都没有明确设置RewriteBase,但the manual entry似乎暗示我不需要它的原因,这也向我建议机制可能与不同的环境变量不同得到设定。)

答案 1 :(得分:0)

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