2010-11-20 63 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