htaccess重写规则冲突


htaccess RewriteRules Colliding

这里有一个真正的脑筋急转弯。一切都很正常,直到我mysite.com/index.php ? page = XXX& parent_url = XXX& child_url = XXX然后我得到这个错误

警告:include(browse/12- clock-bars/cbr-600-f4i.php) [function]。/home/leb/public_html/index.php第167行

警告:include() [function.]在/home/leb/public_html/index.php中打开'browse/12- clock-bars/cbr-600-f4i.php'查找包含(include_path='.:/usr/lib/php')失败

但是前两行运行良好。为什么第三行不适合我的网站。但是如果我注释掉前两行,那么第三行就可以了....我很困惑。

这是我的HTACCESS文件。如有任何帮助,我将不胜感激。
RewriteCond %{HTTP_HOST} ^mysite.com [NC] 
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]

RewriteCond %{REQUEST_URI} !^index.php?
RewriteRule ^(.*)/$ /index.php?page=$1 [L,NC]
RewriteRule ^(.*)/(.*)/$ /index.php?page=$1&parent_url=$2 [L,NC]
RewriteRule ^(.*)/(.*)/(.*)/$ /index.php?page=$1&parent_url=$2&child_url=$3 [L,NC]

<>-------------------------------------------修改了下面的新问题

这里,第3行不能工作,但是其他行都可以,如果我把它的位置向上或向下改变,其他行停止工作。

RewriteRule ^(.*)/(.*)/(.*)/(.*)/$ /index.php?page=$1&parent_url=$2&child_url=$3product=$4 [L,NC]
RewriteRule ^(.*)/(.*)/(.*)/$ /index.php?page=$1&parent_url=$2&child_url=$3 [L,NC]
RewriteRule ^(.*)/(.*)/(.*)/$ /index.php?page=$1&parent_url=$2&product=$3 [L,NC]  <---
RewriteRule ^(.*)/(.*)/$ /index.php?page=$1&parent_url=$2 [L,NC]
RewriteRule ^(.*)/$ /index.php?page=$1 [L,NC]

正如@jeroen评论的那样,规则2和规则3的传入url没有区别。尽管它们应该去的预期页面是不同的,但您在两个规则中检查的传入url仍然是/a/b/c。如果你在URL中有一些指示符,比如规则3,是一个产品:

 RewriteRule ^product/(.*)/(.*)/$ /index.php?page=product&parent_url=$2&product=$3 [L,NC]

有了这个变化,就像你当前的规则一样,这个规则必须在你当前的第二个规则之前。

另外,当匹配这样的部分时,我发现使用([^'/]*)而不是(.*)更容易。这样,该组将匹配直到/的所有内容,并且您将注意到对规则顺序的依赖性更小。

第2行和第3行寻找相同的模式,因此如果匹配,执行将在第2行终止,而不会到达第3行。

我已经得出结论,在PHP中处理url更容易。

你可以用下面的代码在htaccess中传递整个路径:

# pass url path as get variable via "path"
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?path=$1 [L,QSA]

在PHP中,你可以扩展路径:

//Assuming $_GET['path'] = 'this/is/an/example/';
// Trim extra '/' off right side
$path = rtrim($_GET['path'],'/');
//explode the path by "/"
$pathArray = explode("/",path);
echo $pathArray[0]; // outputs "this"
echo $pathArray[1]; // outputs "is"
echo $pathArray[2]; // outputs "an"
echo $pathArray[3]; // outputs "example"
// It is sometimes easier to work with a path in reverse:
$reversedPathArray = array_reverse($pathArray);
$page = $reversedPathArray[0];
echo $page; // outputs "example"

尝试替换 (. *) , ([^/]+):

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ /index.php?page=$1&parent_url=$2&product=$3 [L,NC]

对每一行做同样的操作