2010-11-20 61 views
0

我试图用mod_rewrite工作没有特别的问题,但这样的:mod_rewrite的:避免类似的网址去同page.php文件

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

我该如何解决这个问题?

回答

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"

+0

抱歉,但这似乎并没有工作,我stil我遇到了问题,因为产品名称Wii /组件 – vitto 2010-11-20 13:51:43

+0

您是否添加了[L]标签?否则,它将匹配第一个,然后匹配第二个,并覆盖它... – 2010-11-20 13:53:39

+0

对不起,我的错误,这是非常有用的看到这个例子,我已经修复它正确。 – vitto 2010-11-20 16:01:41

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 
+0

对不起,我不是一个mod_rew和htaccess主,我应该插入像制造商和产品名称的变量? – vitto 2010-11-20 13:51:05

+0

您将在应用程序中而不是在.htaccess中解决这个问题,因为当必须实施更细粒度的规则时,这可以成为PITA。看看我的编辑,它会引导你在正确的方向。 – aefxx 2010-11-20 13:59:55