htaccess不同的行为

时间:2013-11-29 23:04:13

标签: php apache .htaccess

我正在使用以下htaccess:

RewriteEngine on

RewriteBase /

# If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
# If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
# don't do anything
RewriteRule ^ - [L]

# if current ${REQUEST_URI}.php is not a file then
# forward to the parent directory of current REQUEST_URI
RewriteCond %{DOCUMENT_ROOT}/$1/$2.php !-f
RewriteRule ^(.*?)/([^/]+)/?$ $1/?param[]=$2 [L,QSA]

# if current ${REQUEST_URI}.php is a valid file then
# load it be removing optional trailing slash
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

来自问题https://stackoverflow.com/a/20155037/1127807

但是在我的本地机器上它运行得很好但是在我的服务器上根本不起作用并且有不同的环境变量。

在我的本地服务器上,我在$ _SERVER变量中有一些REDIRECT_REDIRECT_变量,在我的服务器上我有'PATH_TRANSLATED'并且没有任何形式的REDIRECT_。

在我的本地服务器上,我会得到一个带有'文件夹'名称的'param',直到找到一个有效的文件,但在我的服务器上我得不到类似的东西。

本地版本:

Apache/2.4.2
PHP 5.4.4

服务器版本:

Apache/2.2.22 (Ubuntu)
PHP 5.3.10-1ubuntu3.8

为了达到同样的效果,我需要做些什么改变?

本地主机

  'server' => 
    array (size=78)
      'REDIRECT_REDIRECT_REDIRECT_REDIRECT_REDIRECT_MIBDIRS' => string '/xampp/php/extras/mibs' (length=22)
      'REDIRECT_REDIRECT_REDIRECT_REDIRECT_REDIRECT_MYSQL_HOME' => string '\xampp\mysql\bin' (length=16)
      'REDIRECT_REDIRECT_REDIRECT_REDIRECT_REDIRECT_OPENSSL_CONF' => string '/xampp/apache/bin/openssl.cnf' (length=29)
      'REDIRECT_REDIRECT_REDIRECT_REDIRECT_REDIRECT_PHP_PEAR_SYSCONF_DIR' => string '\xampp\php' (length=10)
      'REDIRECT_REDIRECT_REDIRECT_REDIRECT_REDIRECT_PHPRC' => string '\xampp\php' (length=10)
      'REDIRECT_REDIRECT_REDIRECT_REDIRECT_REDIRECT_TMP' => string '\xampp\tmp' (length=10)
      'REDIRECT_REDIRECT_REDIRECT_REDIRECT_REDIRECT_STATUS' => string '200' (length=3)
      'REDIRECT_REDIRECT_REDIRECT_REDIRECT_MIBDIRS' => string '/xampp/php/extras/mibs' (length=22)
      'REDIRECT_REDIRECT_REDIRECT_REDIRECT_MYSQL_HOME' => string '\xampp\mysql\bin' (length=16)
      'REDIRECT_REDIRECT_REDIRECT_REDIRECT_OPENSSL_CONF' => string '/xampp/apache/bin/openssl.cnf' (length=29)
      'REDIRECT_REDIRECT_REDIRECT_REDIRECT_PHP_PEAR_SYSCONF_DIR' => string '\xampp\php' (length=10)
      'REDIRECT_REDIRECT_REDIRECT_REDIRECT_PHPRC' => string '\xampp\php' (length=10)
      'REDIRECT_REDIRECT_REDIRECT_REDIRECT_TMP' => string '\xampp\tmp' (length=10)
      'REDIRECT_REDIRECT_REDIRECT_REDIRECT_STATUS' => string '200' (length=3)
      'REDIRECT_REDIRECT_REDIRECT_MIBDIRS' => string '/xampp/php/extras/mibs' (length=22)
      'REDIRECT_REDIRECT_REDIRECT_MYSQL_HOME' => string '\xampp\mysql\bin' (length=16)
      'REDIRECT_REDIRECT_REDIRECT_OPENSSL_CONF' => string '/xampp/apache/bin/openssl.cnf' (length=29)
      'REDIRECT_REDIRECT_REDIRECT_PHP_PEAR_SYSCONF_DIR' => string '\xampp\php' (length=10)
      'REDIRECT_REDIRECT_REDIRECT_PHPRC' => string '\xampp\php' (length=10)
      'REDIRECT_REDIRECT_REDIRECT_TMP' => string '\xampp\tmp' (length=10)
      'REDIRECT_REDIRECT_REDIRECT_STATUS' => string '200' (length=3)
      'REDIRECT_REDIRECT_MIBDIRS' => string '/xampp/php/extras/mibs' (length=22)
      'REDIRECT_REDIRECT_MYSQL_HOME' => string '\xampp\mysql\bin' (length=16)
      'REDIRECT_REDIRECT_OPENSSL_CONF' => string '/xampp/apache/bin/openssl.cnf' (length=29)
      'REDIRECT_REDIRECT_PHP_PEAR_SYSCONF_DIR' => string '\xampp\php' (length=10)
      'REDIRECT_REDIRECT_PHPRC' => string '\xampp\php' (length=10)
      'REDIRECT_REDIRECT_TMP' => string '\xampp\tmp' (length=10)
      'REDIRECT_REDIRECT_STATUS' => string '200' (length=3)
      'REDIRECT_MIBDIRS' => string '/xampp/php/extras/mibs' (length=22)
      'REDIRECT_MYSQL_HOME' => string '\xampp\mysql\bin' (length=16)
      'REDIRECT_OPENSSL_CONF' => string '/xampp/apache/bin/openssl.cnf' (length=29)
      'REDIRECT_PHP_PEAR_SYSCONF_DIR' => string '\xampp\php' (length=10)
      'REDIRECT_PHPRC' => string '\xampp\php' (length=10)
      'REDIRECT_TMP' => string '\xampp\tmp' (length=10)
      'REDIRECT_STATUS' => string '200' (length=3)
      'MIBDIRS' => string '/xampp/php/extras/mibs' (length=22)
      'MYSQL_HOME' => string '\xampp\mysql\bin' (length=16)
      'OPENSSL_CONF' => string '/xampp/apache/bin/openssl.cnf' (length=29)
      'PHP_PEAR_SYSCONF_DIR' => string '\xampp\php' (length=10)
      'PHPRC' => string '\xampp\php' (length=10)
      'TMP' => string '\xampp\tmp' (length=10)
      'HTTP_HOST' => string 'site.dev' (length=13)
      'HTTP_CONNECTION' => string 'keep-alive' (length=10)
      'HTTP_CACHE_CONTROL' => string 'max-age=0' (length=9)
      'HTTP_ACCEPT' => string 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' (length=74)
      'HTTP_USER_AGENT' => string 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36' (length=108)
      'HTTP_ACCEPT_ENCODING' => string 'gzip,deflate,sdch' (length=17)
      'HTTP_ACCEPT_LANGUAGE' => string 'en-US,en;q=0.8' (length=14)
      'HTTP_COOKIE' => string '__atuvc=122%7C45%2C160%7C46%2C409%7C47%2C53%7C48' (length=48)
      'PATH' => string ''... (length=559)
      'SystemRoot' => string 'C:\Windows' (length=10)
      'COMSPEC' => string 'C:\Windows\system32\cmd.exe' (length=27)
      'PATHEXT' => string '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC' (length=53)
      'WINDIR' => string 'C:\Windows' (length=10)
      'SERVER_SIGNATURE' => string '<address>Apache/2.4.2 (Win32) OpenSSL/1.0.1c PHP/5.4.4 Server at site.dev Port 80</address>
' (length=97)
      'SERVER_SOFTWARE' => string 'Apache/2.4.2 (Win32) OpenSSL/1.0.1c PHP/5.4.4' (length=45)
      'SERVER_NAME' => string 'site.dev' (length=13)
      'SERVER_ADDR' => string '127.0.0.1' (length=9)
      'SERVER_PORT' => string '80' (length=2)
      'REMOTE_ADDR' => string '127.0.0.1' (length=9)
      'DOCUMENT_ROOT' => string 'Site/Public_domain' (length=56)
      'REQUEST_SCHEME' => string 'http' (length=4)
      'CONTEXT_PREFIX' => string '' (length=0)
      'CONTEXT_DOCUMENT_ROOT' => string 'Site/Public_domain' (length=56)
      'SERVER_ADMIN' => string 'postmaster@site.dev' (length=24)
      'SCRIPT_FILENAME' => string 'Site/Public_domain/i.php' (length=62)
      'REMOTE_PORT' => string '60593' (length=5)
      'REDIRECT_QUERY_STRING' => string 'param[]=folder&param[]=file&param[]=foo&param[]=bar&bar=foo' (length=59)
      'REDIRECT_URL' => string '/i/' (length=3)
      'GATEWAY_INTERFACE' => string 'CGI/1.1' (length=7)
      'SERVER_PROTOCOL' => string 'HTTP/1.1' (length=8)
      'REQUEST_METHOD' => string 'GET' (length=3)
      'QUERY_STRING' => string 'param[]=folder&param[]=file&param[]=foo&param[]=bar&bar=foo' (length=59)
      'REQUEST_URI' => string '/i/folder/file/foo/bar?bar=foo' (length=30)
      'SCRIPT_NAME' => string '/i.php' (length=6)
      'PHP_SELF' => string '/i.php' (length=6)
      'REQUEST_TIME_FLOAT' => float 1385767068.811
      'REQUEST_TIME' => int 1385767068

服务器

  ["server"]=>
  array(28) {
    ["HTTP_HOST"]=>
    string(10) "*****"
    ["HTTP_CONNECTION"]=>
    string(10) "keep-alive"
    ["HTTP_ACCEPT"]=>
    string(74) "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
    ["HTTP_USER_AGENT"]=>
    string(108) "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36"
    ["HTTP_ACCEPT_ENCODING"]=>
    string(17) "gzip,deflate,sdch"
    ["HTTP_ACCEPT_LANGUAGE"]=>
    string(14) "en-US,en;q=0.8"
    ["HTTP_COOKIE"]=>
    string(14) "__atuvc=6%7C48"
    ["PATH"]=>
    string(28) "/usr/local/bin:/usr/bin:/bin"
    ["SERVER_SIGNATURE"]=>
    string(0) ""
    ["SERVER_SOFTWARE"]=>
    string(6) "Apache"
    ["SERVER_NAME"]=>
    string(10) "****"
    ["SERVER_ADDR"]=>
    string(11) "****"
    ["SERVER_PORT"]=>
    string(2) "80"
    ["REMOTE_ADDR"]=>
    string(14) "****"
    ["DOCUMENT_ROOT"]=>
    string(56) "/var/www/site/Public_domain"
    ["SERVER_ADMIN"]=>
    string(16) "***"
    ["SCRIPT_FILENAME"]=>
    string(62) "/var/www/site/Public_domain/i.php"
    ["REMOTE_PORT"]=>
    string(5) "50577"
    ["GATEWAY_INTERFACE"]=>
    string(7) "CGI/1.1"
    ["SERVER_PROTOCOL"]=>
    string(8) "HTTP/1.1"
    ["REQUEST_METHOD"]=>
    string(3) "GET"
    ["QUERY_STRING"]=>
    string(7) "bar=foo"
    ["REQUEST_URI"]=>
    string(30) "/i/folder/file/foo/bar?bar=foo"
    ["SCRIPT_NAME"]=>
    string(6) "/i.php"
    ["PATH_INFO"]=>
    string(20) "/folder/file/foo/bar"
    ["PATH_TRANSLATED"]=>
    string(39) "redirect:/folder/file/foo//file/foo/bar"
    ["PHP_SELF"]=>
    string(26) "/i.php/folder/file/foo/bar"
    ["REQUEST_TIME"]=>
    int(1385767075)
  }

提前致谢

1 个答案:

答案 0 :(得分:0)

根据我的研究,这似乎是Apache中的一个注册错误。在此处阅读更多相关信息:What is redirect:// in PATH_TRANSLATED?

修复,请使用 NS标记尝试此代码:

RewriteEngine on
RewriteBase /

# If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
# If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
# don't do anything
RewriteRule ^ - [L,NS]

# if current ${REQUEST_URI}.php is not a file then
# forward to the parent directory of current REQUEST_URI
RewriteCond %{DOCUMENT_ROOT}/$1/$2.php !-f
RewriteRule ^(.*?)/([^/]+)/?$ $1/?param[]=$2 [L,QSA,NS]

# if current ${REQUEST_URI}.php is a valid file then
# load it be removing optional trailing slash
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L,NS]