2009-06-10 4 views
3

SEO 친화적 인 URL의 I 변환하려면 어떻게

me.com/profile/24443/quincy-jones 

me.com/profile.php?id=24443

이나 뭐 같은

me.com/store/24111/robert-adams

같은

me.com/store.php?id=24111 

(mod_rewrite 포함)?

mod_rewrite를 사용하여 역방향 변환을 수행 할 수 있습니까? 아니면 PHP를 통해 구문 분석해야합니까?

답변

9

이 모두 작동합니다 :

RewriteEngine on 
RewriteRule ^([^/]+)/([^/]+).*$ $1.php?id=$2 [L] 

설명 :

^   - beginning of the string 
([^/])  - first group that doesn't contain/
       will match both 'profile' and 'store' 
       will also be referenced by $1 later 
/   - first slash separator 
([^/])  - second group, id in your case 
       will be referenced by $2 
.*   - any ending of the request uri 
$   - end of request string 
당신은 또한 더 정확한 수

그래서 오직 두 개의 요청을 다시 작성하고 단지 숫자 ID로 허용됩니다

RewriteRule ^((profile|store))/(\d+).*$ $1.php?id=$2 [L] 
+0

+1, 설명 – Starx

2

mod_rewrite apache 모듈이 활성화되어 있는지 확인한 다음 :

RewriteEngine on 

RewriteRule ^/profile/([^/]*)/([^/]*)$ /profile.php?id=$1 [L] 

RewriteRule ^/store/([^/]*)/([^/]*)$ /store.php?id=$1 [L] 

PHP에서 역순으로 처리해야 할 수도 있습니다. 특히 원래의 이름 부분에는 후행 이름 부분이 있습니다. 이름없이 mod_rewrite에서 처리하려면, 규칙의 순서에 따라 두 번 다시 작성하지 않도록하십시오. 또한 [L] (마지막) 스위치를 사용하여 마지막으로 사용한 규칙을 사용할 수 있습니다 (일치하는 항목이 있으면 후속 규칙을 건너 뜁니다).

또한보다 일반적인 다시 쓰기 규칙을 만들 수도 있지만 영향을받을 수있는 다른 URL에 대해 신중하게 고려해야합니다.

관련 문제