2010-12-29 9 views
0

다음 규칙을 원하지만 올바른 설정을 얻지 못했습니다..htaccess에서 여러 mod 다시 쓰기

가 이동해야하거나 끝에 슬래시없이 모두

<domain>/training-courses/

:

<domain>/?index.php?page=training-courses

이 후 각 변수 추가를 위해 내가이처럼 행동 할 :

<domain>/training-courses/success/another-value/and-yet-another/

to

이 무제한 주요 변수에 대한 옵션을 가질 수는 없습니다 경우

<domain>/?index.php?page=training-courses&val1=success&val2=another-value&val3=and-yet-another-value

, 내가 변수 페이지

후 최소 2 개 변수를 가지고 싶습니다이 가능합니까? 어떻게 정리해야합니까?

지금까지이 있습니다 방식에 하위 폴더가 당신이 mod_rewrite를 그냥 지원하지 않습니다 싶은대로 매우 맞아요

RewriteEngine On 

RewriteRule ^test/([^/]*)/$ /test/index.php?pagina=$1&val1=$2 
RewriteRule ^test/([^/]*)$ /test/index.php?pagina=$1&val1=$2 

RewriteRule ^test/([^/]*)/([^/]*)/$ /test/index.php?pagina=$1&val1=$2 
RewriteRule ^test/([^/]*)/([^/]*)$ /test/index.php?pagina=$1&val1=$2 

RewriteRule ^test/([^/]*)/([^/]*)/([^/]*)/$ /test/index.php?pagina=$1&val1=$2&val2=$3 
RewriteRule ^test/([^/]*)/([^/]*)/([^/]*)$ /test/index.php?pagina=$1&val1=$2&val2=$3 

답변

0

나는 내 자신의 질문에 답하고 싶습니다. 잠시 동안 나는 내가 원하는 것을 정확하게 사용하고 있습니다.

이 꺼 내 htaccess로이 있습니다 첫째 :

<IfModule mod_rewrite.c> 

    RewriteEngine On 

    # existing folders or files 
    RewriteCond %{REQUEST_FILENAME} -f [OR] 
    RewriteCond %{REQUEST_FILENAME} -d 
    RewriteRule ^.* - [L] 

    # non-existing folders or files 
    RewriteRule ^(.*)/? index.php?p=$1 [L] 

</IfModule> 

이 항상 실제 파일에 액세스 할 수 있습니다. 경로가 존재하지 않으면 index.php에 전체 경로를 전달합니다.

의 index.php에서 나는 맨 위에 다음 코드를 가지고 :

// Split the URL in all its components 
$request = parse_url(strip_tags($_GET['p'])); 
// Only take the path without any leading slashes 
$path = rtrim($request['path'], '/'); 
// Split each variable from eachother 
$path_array = explode("/", $path); 

if(count($path_array)){ 
    // By default I select the first variable as the page I will include later on 
    $page = $path_array[0]; 

    // Each other variable I will pass as $val[#nr] starting with 1. Just to be sure I'm going to strip tags and slashes 
    for($i=1;$i<count($path_array);$i++){ 
     $path_array[$i] = strip_tags($path_array[$i]); 
     $path_array[$i] = stripslashes($path_array[$i]); 
     $val[$i] = $path_array[$i]; 
    } 
} 
else{ 
    // If $path_array doesn't contain multiple variables lets assume $path will contain the $page 
    $page = $path; 
} 

// If $page is empty let's take on the default page to include later on 
if(empty($page)){ 
    $page = "home"; 
} 

if(file_exists($page . ".php")){ 
    include($page . ".php"); 
} 
else{ 
    echo "Page " . $page . ".php does not exist."; 
} 

는 당신에게 posibilities의 몇 가지 예를 제공하기 위해 :

domain.com/이 home.php 도메인이 포함됩니다.com/product /는 product.php를 포함합니다 domain.com/product/298833/은 product.php를 포함하고 $ val [1];로 298833을 분석합니다. domain.com/product/298833/purple-candle은 product.php를 포함하고 $ val [1];로 298833을 파싱합니다. $ val [2]와 같은 보라색 촛불; 선행 슬래시가 포함되지 않음을 주목하십시오. 그것은 중요하지 않습니다.

나는 개선이나 일반적으로 도움이 필요합니다. 당분간이 질문을 할 때 내가 찾고 있던 답이 틀림 없습니다.

0

, 당신은 무제한 변수를 처리 할 수 ​​없습니다.

그러나, 첫 번째 문제에 대한 대답, 후행가/옵션 만들려면 다음과 규칙을 변경 :

RewriteRule ^test/([^/]*)/?$ /test/index.php?pagina=$1 

RewriteRule ^test/([^/]*)/([^/]*)/?$ /test/index.php?pagina=$1&val1=$2 

RewriteRule ^test/([^/]*)/([^/]*)/([^/]*)/?$ /test/index.php?pagina=$1&val1=$2&val2=$3 
+0

정확히 "내부 서버 오류"가 발생하지만 후행 슬래시에 대한 팁 주셔서 감사합니다. – Bob

+0

아, 미안하지만 게시하기 전에 빠른 테스트를 수행해야합니다. –

0

시간하고 struggeling 시간 그냥 작동하지 않습니다 후. 무제한 변수가 지원되지 않기 때문에 페이지 (pagina) 외에 두 개의 변수가 있어도 괜찮습니다. 의 RewriteRules의 다른의 RewriteRules에서 폴더를 제외 처음부터 제외 작동

RewriteEngine On 

RewriteRule ^(test/images|test/css|test/js|test/lib)($|/) - [L] 

RewriteRule ^test/(.*)/(.*)/(.*)/ test/index.php?pagina=$1&e=$2&t=$3 
RewriteRule ^test/(.*)/(.*)/(.*) test/index.php?pagina=$1&e=$2&t=$3 
RewriteRule ^test/(.*)/(.*)/ test/index.php?pagina=$1&e=$2 
RewriteRule ^test/(.*)/(.*) test/index.php?pagina=$1&e=$2 
RewriteRule ^test/(.*)/ test/index.php?pagina=$1 
RewriteRule ^test/(.*) test/index.php?pagina=$1 

없음 : 내가 지금 가지고있는 것은 이것이다. 작동하면 실제로 URL을 index.php로 리디렉션 할 것입니다. 그러나 PHP에서 값을 가져올 수 없습니다. 예를 들어 $ _GET [ 'pagina']

이상한 일은 너무 제한적이지만 내가 좋아하는 것 :

RewriteEngine On 

RewriteRule ^(test/images|test/css|test/js|test/lib)($|/) - [L] 

RewriteRule ^test/(.*)/ test/index.php?pagina=$1 
관련 문제