mod_rewrite:避免类似的URL转到同一个page.php

时间:2010-11-20 13:28:51

标签: regex .htaccess mod-rewrite

我正在尝试使用mod_rewrite而没有遇到任何特殊问题,但是:

RewriteRule ^(product/)([^/\<\>].+?)(/)([^/\<\>].+?)(/edit)/?$             php/prd_edit.php
RewriteRule ^(product/)([^/\<\>].+?)(/)([^/\<\>].+?)(/components/edit)/?$  php/prd_comp_edit.php
当我想编辑主要信息时,first URL就是这种内容:

http://site.com/product/Nintento/Wii/edit => php/prd_edit.php

second URL是我想要编辑产品中的组件列表

http://site.com/product/Nintento/Wii/components/edit => php/prd_comp_edit.php

如果我将second URL数字化,mod_rewrite将请求重定向到prd_edit.php而不是prd_comp_edit.php,因为它将Wii/components视为产品名称。

我希望通过使用([^/\<\>].+?)来指明产品不能包含/这样的字符,但它仍然将产品名称读作Wii/components

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

首先,首先移动第二次重写并向其添加[L]标志,这将解决您的问题,这里有一些更优化。

RewriteRule ^product/(.+?)/(.+?)/components/edit/?$ php/prd_comp_edit.php?cat1=$1&cat2=$2 [L]
RewriteRule ^product/(.+?)/(.+?)/edit/?$ php/prd_edit.php?cat1=$1&cat2=$2 [L]

现在,举个例子,$_GET['cat1'] == "Nintendo"$_GET['cat2'] == "wii"

答案 1 :(得分:0)

RewriteCond %{REQUEST_URI} ^/product
RewriteRule /components/edit/?$ php/prd_comp_edit.php [L]
RewriteRule /edit/?$ php/prd_edit.php [L]

这样您就可以拥有更深层次的嵌套产品组:D

修改

$uri = $_SERVER['REQUEST_URI'];

// Split the URI apart using explode or any other suitable method
$parts = explode('/', rtrim($uri, '/'));

// Now throw away any part that is not connected to a product
// You know that the URI started with product and also know
// that it ended with components and/or edit

// $x = file is prd_comp_edit.php ? 3 : 2
$parts = array_slice($parts, 1, count($parts) - $x);

// Now there you have it, the products hierarchy ready to be processed further