Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将请求URI从htaccess传递到PHP_Php_.htaccess_Mod Rewrite - Fatal编程技术网

如何将请求URI从htaccess传递到PHP

如何将请求URI从htaccess传递到PHP,php,.htaccess,mod-rewrite,Php,.htaccess,Mod Rewrite,我正在设置一个重定向URL,其中将请求URI从.htaccess传递到PHP文件 SetEnv RURI %{REQUEST_URI} 以下是我的文件结构: index.php <?php $requestURI = getenv('RURI'); ?> .htaccess SetEnv RURI %{REQUEST_URI} folder/index.php <?php $requestURI = getenv('RURI'); ?> 如果URL是http:

我正在设置一个重定向URL,其中将
请求URI
.htaccess
传递到PHP文件

SetEnv RURI %{REQUEST_URI}
以下是我的文件结构:

  • index.php
  • <?php $requestURI = getenv('RURI'); ?>
    
  • .htaccess
  • SetEnv RURI %{REQUEST_URI}
    
  • folder/index.php
  • <?php $requestURI = getenv('RURI'); ?>
    
如果URL是
http://example.com/?something

我想将
%{REQUEST\u URI}
完整URL从
.htaccess
传递到
文件夹/index.php
,并使用
$\u服务器['REQUEST\u URI']
检索它

SetEnv RURI %{REQUEST_URI}
<?php $requestURI = getenv('RURI'); ?>
我的
文件夹/index.php

echo $_SERVER['REQUEST_URI'];
<?php $requestURI = getenv('RURI'); ?>
.htaccess

SetEnv RURI %{REQUEST_URI}
更新:

Options +FollowSymLinks -MultiViews
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{QUERY_STRING} ^(.*)$
    RewriteCond %{REQUEST_URI} !/folder/index.php$
    RewriteRule .* /folder/index.php [L,NE]
</IfModule>
Options+FollowSymLinks-多视图
重新启动发动机
RewriteCond%{QUERY_STRING}^(.*)$
重写cond%{REQUEST_URI}/folder/index.php$
重写规则。*/folder/index.php[L,NE]

我希望在PHP文件中回显
请求\u URI
,但总是给外部服务器。

使用环境变量

.htaccess
SetEnv RURI %{REQUEST_URI}
index.php
<?php $requestURI = getenv('RURI'); ?>

确保在Apache中启用了
mod_env

我想将
%{REQUEST\u URI}
完整URL从
.htaccess
传递到
文件夹/index.php
,并使用
$\u服务器['REQUEST\u URI']
检索它

SetEnv RURI %{REQUEST_URI}
<?php $requestURI = getenv('RURI'); ?>
你不能真的这么做(但我不认为这是你正在尝试做的,或者甚至不想做的)。为了澄清

PHP超级全局
$\u服务器['REQUEST\u URI']
由PHP填充,无法重写

虽然
%{REQUEST\u URI}
(Apache服务器变量)和
$\u服务器['REQUEST\u URI']
(PHP超全局)类似,但它们引用的值不同:

  • %{REQUEST_URI}
    (Apache服务器变量)-仅引用URL路径(无查询字符串)。该值已解码。如果请求被重写,那么它将更新为包含重写的URL,而不是初始请求的URL

  • $\u服务器['REQUEST\u URI']
    (PHP超级全局)-保存用户请求的初始URL路径和查询字符串(而不是重写的URL)。该值未被解码

如果您确实希望在PHP中引用Apache服务器变量
REQUEST\u URI
,那么您需要显式地传递它(可能作为环境变量或查询字符串),但根据“传递”的时间,最终可能传递不同的值。不管怎样,这都不会改变
$\u SERVER['REQUEST\u URI']
的值,如上所述,它是由PHP控制的

例如:

RewriteRule ^ - [E=APACHE_REQUEST_URI:%{REQUEST_URI}]
并在PHP中将其引用为
getenv('APACHE\u REQUEST\u URI')


更新:

Options +FollowSymLinks -MultiViews
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{QUERY_STRING} ^(.*)$
    RewriteCond %{REQUEST_URI} !/folder/index.php$
    RewriteRule .* /folder/index.php [L,NE]
</IfModule>
听上去,您可能想要以下内容:

RewriteCond %{QUERY_STRING} .
RewriteRule ^(index\.php)?$ folder/index.php [L,NE]
这会在内部将包含查询字符串(位于
之后的所有内容)的文档根请求重写为
/folder/index.php
。当查询字符串不存在时,文档根中的
index.php
将正常使用

<?php $requestURI = getenv('RURI'); ?>

然后,在
/folder/index.php
中,您只需访问
$\u服务器['QUERY\u STRING']
即可访问查询字符串(要重定向到的字符串)。

已测试但不起作用,你必须知道我在main index.php中使用的是什么,而不是folder/index.php。我认为我的方法是50%正确的,但不知道缺少什么才能使它完全工作。尝试将
SetEnv
放在
RewriteCond
之前。抱歉,但不起作用。有没有办法使用$u SERVER['REQUEST\u URI']将其放入我的php文件夹/index.php中?当键入mydomain.com/?时,它会提供500个外部服务器页面,而不是将请求\u URI从htaccess传递到php文件。通常您不需要.htaccess来获取该变量值。
$SERVER[”REQUEST_URI“]
获取它。只需将包含该指令的脚本放在/public_html或类似目录“我尝试了你的代码,但不起作用”-我发布的代码确实起作用,但它并不是为了解决你的特定问题。这听起来像是一个问题?你实际上在尝试做什么?”我想传递完整的
$\u服务器['REQUEST_URI']
到我的PHP文件”-这没有意义。如上所述,这是由PHP自动生成的,没有什么需要“传递”?并且不需要解析它来获得“url after
”-这被称为查询字符串,并且已经存在于PHP superglobal
$\u服务器['query\u string']
。我想我明白了你的意思。我已经更新了我的答案。(询问是否通过
请求\u URI
似乎是一个问题。)很高兴它能工作,不客气。请将其标记为“已接受”(选中投票箭头下方的复选标记)将其从未回答的问题队列中删除。您也可以将您发现有用的答案向上投票。但这是正确的吗?然后,您可以使用类似以下内容提取查询字符串:
parse\u url($\u SERVER['REQUEST\u URI'],PHP\u url\u query);
substr(strstr($\u SERVER['REQUEST\u URI'],'?'),1)
-两者的结果相同,例如
http://google.com
。是的,您的代码现在可以正常工作了,非常感谢您的帮助:)
<?php $requestURI = getenv('RURI'); ?>