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
Php htaccess中的规则冲突_Php_.htaccess_Mod Rewrite_Url Rewriting_Rewrite - Fatal编程技术网

Php htaccess中的规则冲突

Php htaccess中的规则冲突,php,.htaccess,mod-rewrite,url-rewriting,rewrite,Php,.htaccess,Mod Rewrite,Url Rewriting,Rewrite,我正在和我的班一起做一个htaccess文件似乎不起作用的项目。我有这个: SetEnvIf Request_URI ^ root=/myroot/ RewriteRule ^(assets|inc) - [L] RewriteRule ^section[/]?$ %{ENV:root}section/index RewriteRule ^section/function/(.*)$ %{ENV:root}index.php?type=section&action=functi

我正在和我的班一起做一个htaccess文件似乎不起作用的项目。我有这个:

SetEnvIf Request_URI ^ root=/myroot/

RewriteRule ^(assets|inc) - [L]
RewriteRule ^section[/]?$ %{ENV:root}section/index    
RewriteRule ^section/function/(.*)$ %{ENV:root}index.php?type=section&action=function [L]
RewriteCond %{QUERY_STRING} ^(.*)?$
RewriteRule ^section/(.*)$                      %{ENV:root}index.php?type=section&action=view [L]
RewriteRule ^index\.php$ - [L]
事实上,有三件事我不明白:

如果我以
/section/function/
的形式查询url,我应该得到/myroot/index.php?type=section&action=function,但我在浏览器中得到
localhost/section/function
,从而导致错误。url是否双向翻译:myroot/index.php?type=section&action=function在浏览器中显示为
/section/function/
。如果我删除最后一行,我会得到空白的错误页,为什么

编辑:

因此,与奥拉夫·迪彻所说的一致,我将规则改为

SetEnvIf请求_URI ^root=/myroot/

RewriteRule ^(assets|inc) - [L]
RewriteRule ^section[/]?$ %{ENV:root}section/index    
RewriteRule ^section/function/(.*)$ %{ENV:root}?type=section&action=function [L]
RewriteCond %{QUERY_STRING} ^(.*)?$
RewriteRule ^section/(.*)$                      %{ENV:root}?type=section&action=view [L]
RewriteRule ^index\.php$ - [L]
但是当我点击
,我会看到一个页面
localhost/section/somefunction
。我真的不知道出了什么问题……首先,你必须

RewriteEngine on
在.htaccess中,规则才能工作

规则重写请求,但不重定向客户端。两者的区别在于

  • 重写将另一页的内容发送到客户端,而不告诉客户端。客户认为,它获得了请求页面的内容
  • 重定向向客户端发送另一个URL,使客户端使用此URL发出新请求。客户端知道(并显示在浏览器的栏中)它收到了另一个页面的内容
如果要重定向客户端,必须在
重写规则中添加一个标志

RewriteRule ^section/function/(.*)$ %{ENV:root}index.php?type=section&action=function [R,L]
更新

您必须在替换之前删除
%{ENV:root}
,因为URL是例如
/index.php
而不是
/myroot/index.php

RewriteRule ^section[/]?$ /section/index    
RewriteRule ^section/function/(.*)$ /index.php?type=section&action=function [L]
RewriteCond %{QUERY_STRING} ^(.*)?$
RewriteRule ^section/(.*)$ /index.php?type=section&action=view [L]
RewriteRule ^index\.php$ - [L]

谢谢你的解释。当然,我确实将RewriteEngine设置为on,但它仍然不工作,为什么会这样?@user1611830我在测试环境中尝试了这些规则,它们工作了,将
/section/function
重写为
/myroot/index.php
。如果没有error.log消息,很难判断。例如,如果
/myroot/
不是子目录而是文档根目录,则不能将其前置到
index.php
。如果它是一个子目录,那么其中可能没有
index.php
。PHP脚本本身可能有一些错误,等等……对不起,我不想打扰您。但实际上,/myroot是根文件夹,所以我更新了我的规则(请参阅我的规则),但它没有改变anything@user1611830对不起,我不太清楚。请参阅更新的答案。我添加了R重定向规则,但当我单击时,仍然会看到页面localhost/section/somefunction