htaccess 重写规则冲突

发布于 2024-11-04 18:59:26 字数 1454 浏览 0 评论 0原文

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

警告:包括(浏览/12-oclock-bars/cbr-600-f4i.php)[function.include]:无法打开流:/home/leb/public_html/index中没有这样的文件或目录.php 第 167 行

警告:include() [function.include]:无法打开 'browse/12-oclock-bars/cbr-600-f4i.php' 进行包含 (include_path='.:/usr/lib/php' )在 /home/leb/public_html/index.php 第 167 行

但是前两行工作正常。为什么第三行不适用于我的网站。但是如果我注释掉前两行,那么第三行就可以了......我很困惑。

这是我的 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]

Having a real brain teaser here.
Everything works fine like it should until I get to
mysite.com/index.php?page=XXX&parent_url=XXX&child_url=XXX
Then I get this error

Warning: include(browse/12-oclock-bars/cbr-600-f4i.php) [function.include]: failed to open stream: No such file or directory in /home/leb/public_html/index.php on line 167

Warning: include() [function.include]: Failed opening 'browse/12-oclock-bars/cbr-600-f4i.php' for inclusion (include_path='.:/usr/lib/php') in /home/leb/public_html/index.php on line 167

But the first two lines are working fine. Why would the 3rd line not work for my site. But if I comment out the first two lines, than the third line works.... I'm so confused.

Here is my HTACCESS file. Any help would be greatly appreciated.

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]

-------------------------------------------
Revised with new problem below

Here, Line 3 won't work, but all the others do, If I change its position up or down, the others stop working.

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]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

揪着可爱 2024-11-11 18:59:26

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

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

随着该更改,正如您当前的规则一样,该规则必须位于当前的第二条规则之前。

另外,当匹配这样的部分时,我发现使用 ([^\/]*) 而不是 (.*) 更容易。这样,该组就会匹配 / 之前的所有内容,并且您会注意到对规则顺序的依赖减少了。

希望这有帮助。

As @jeroen commented, there is no difference in the incoming URLs for rules 2 and 3. Although the intended page they are supposed to go to is different, the incoming URLs you are checking for in both rules is still /a/b/c. If you had some indicator in the URL, say for Rule 3, being a product:

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

With that change, as your rules are currently, that rule would have to come before your current 2nd rule.

Also, when matching sections like this, I find it much easier to use ([^\/]*) instead of (.*). This way, the group matches everything up until the / and you will notice less dependence on the ordering of your rules.

Hope this helps.

┈┾☆殇 2024-11-11 18:59:26

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

Line 2 and line 3 look for the same pattern, so execution will terminate at line 2 and never reach line 3 in case of a match.

爱的十字路口 2024-11-11 18:59:26

我得出的结论是,在 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"

I have come to the conclusion that it is easier to deal with the url in PHP.

You can pass the whole path with the following code in htaccess:

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

In PHP you can explode the path:

//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"
我的影子我的梦 2024-11-11 18:59:26

尝试将 (.*) 替换为 ([^/]+)

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

并对每一行执行相同的操作。

Try replacing (.*) by ([^/]+):

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

And do the same thing for every line.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文