2010-11-20 5 views
0

mod_rewrite : 비슷한 URL이 같은 page.php로가는 것을 피하십시오

first URL 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 

제품 번호로 Wii/components를 읽었 기 때문에 second URL을 숫자로 표시하면 mod_rewriteprd_edit.php 대신 prd_edit.php으로 리디렉션됩니다.

제품을 /와 같은 문자를 사용할 수 없도록 지정하기 위해 ([^/\<\>].+?)을 사용하여이 문제를 피할 수 있지만 제품 이름은 여전히 ​​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

미안하지만이 기능이 작동하지 않는 것 같습니다. 내가 제품 이름 Wii/components로 얻는 문제가있다 – vitto

+0

[L] 태그를 추가 했습니까? 그렇지 않으면 첫 번째 것과 일치하고 두 번째 것과 일치하고 ... –

+0

죄송합니다, 실수입니다.이 예제를 보면 매우 유용합니다. 올바르게 수정했습니다. – vitto

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 마스터가 아니므로 제조업체 및 제품 이름과 같은 vars를 삽입해야합니까? – vitto

+0

더 세밀한 규칙을 구현해야하는 경우 PITA가 될 수 있으므로 .htaccess가 아닌 응용 프로그램에서이 문제를 처리 할 수 ​​있습니다. 내 EDIT를 보면 올바른 방향으로 인도 할 것입니다. – aefxx

관련 문제