2009-06-30 4 views
0

간단한 URL 재 작성 설정에 .htaccess 파일이 있습니다. 발췌 :mod_rewrite URL을 역순으로 매핑 할 수 있습니까?

RewriteEngine On 

RewriteRule ^home(/)?$   index.php?mod=frontpage&com=showFrontpage 
RewriteRule ^signup(/)?$   index.php?mod=usermanager&com=showRegistrationForm 

잘 작동합니다. 그러나 이전 스타일 URL에 대한 요청도 마찬가지입니다. 그런 요청이 들어 오면 나는 SEO 친화적 인 URL에 대한 301 영구 리디렉션을 수행하려고하지만 /index.php?mod=frontpage&com=showFrontpage/home으로 매핑하는 방법을 알 수없는 것 같습니다. .htaccess 파일을 구문 분석하고 정규식 해킹을해야합니까?

URL 재 작성이 프로젝트의 꽤 늦게 소개되었으므로 PHP 스크립트는 URL 재 작성을 '인식하지 못합니다. 어느면에서 phpcode

를 통해 htaccess로

  • 를 통해

    1. 다음 .htaccess 파일

  • 답변

    0

    누구든지 관심이 있으신 경우,이 PHP 코드를 사용하여 직접 해결할 수 있습니다.

    class clsURL { 
        static function checkURL() { 
         // don't allow requests on old style URL's 
         if (($_SERVER['SCRIPT_NAME'] . '?' . $_SERVER['QUERY_STRING']) == $_SERVER['REQUEST_URI'] && 
           $_SERVER['REQUEST_METHOD'] != 'POST') { 
    
          // redirect to new style URL 
          $redirectTable = self::createReverseTable(); 
          $currentURL = $_SERVER['REQUEST_URI']; 
          if (substr($currentURL, 0, 1) == "/") 
           $currentURL = substr($currentURL, 1); 
          $newURL = self::getReverseURL($currentURL, $redirectTable); 
    
          if ($newURL) { 
           header ('HTTP/1.1 301 Moved Permanently'); 
           header ('Location: /' . $newURL); 
    
           exit; 
          } 
         } 
        } 
    
        static function getReverseURL($current, $reverseTable) { 
         // filter out some common stuff 
         $current = preg_replace("/&mid=[0-9]+/", "", $current); 
    
         foreach ($reverseTable as $pair) { 
          if (preg_match("|" . $pair['from'] . "|", $current)) { 
           return preg_replace("|" . $pair['from'] . "|", $pair['to'], $current); 
          } 
         } 
    
         // nothing found 
         return false; 
        } 
    
        static function createReverseTable() { 
         $file = fopen('.htaccess', 'r'); 
         $reverse = array(); 
    
         while ($line = fgets($file)) { 
          if (stripos($line, 'RewriteRule') === 0) { 
           $parts = preg_split("/[\\s\\t]/", $line, 3, PREG_SPLIT_NO_EMPTY); 
    
           $regex = trim($parts[1]); 
           $url = trim($parts[2]); 
           $parts[2] = $url; 
    
           $matches = array(); 
           if (preg_match_all("/\\$[0-9]/", $url, $matches)) { 
            $matches = $matches[0]; // why? don't know. 
            $from = str_replace('?', '\\?', $url); 
            $to = $regex; 
    
            foreach ($matches as $match) { 
             $index = substr($match, 1); 
             $bracket = 0; 
    
             for ($i = 0; $i < strlen($regex); ++$i) { 
              if (substr($regex, $i, 1) == "(") { 
               $bracket++; 
    
               if ($bracket == $index) { 
                $pattern = ""; 
    
                $j = $i + 1; 
                while (substr($regex, $j, 1) != ")") { 
                 $pattern .= substr($regex, $j, 1); 
                 $j++; 
                } 
    
                $from = str_replace('$' . $index, '(' . $pattern . ')', $from); 
                $to = preg_replace('/\\(' . preg_quote($pattern, '/') . '\\)/', '\\$' . $index, $to, 1); 
               } 
              } 
             } 
            } 
    
            // remove optional stuff that we don't use 
            $to = str_replace('(-(.*))?', '', $to); 
    
            // remove^and (/)?$ 
            $to = substr($to, 1); 
            if (substr($to, -5) == '(/)?$') 
             $to = substr($to, 0, strlen($to) - 5); 
            $from = '^' . $from . '$'; 
    
            // index.php is optional 
            $from = str_replace('index.php', '(?:index\\.php)?', $from); 
    
            $reverse[] = array(
             'from' => $from, 
             'to' => $to 
            ); 
           } else { 
            $from = str_replace('?', '\\?', $url); 
            $to = $regex; 
    
            // remove^and (/)?$ 
            $to = substr($to, 1); 
            if (substr($to, -5) == '(/)?$') 
             $to = substr($to, 0, strlen($to) - 5); 
            $from = '^' . $from . '$'; 
    
            // index.php is optional 
            $from = str_replace('index.php', '(?:index\\.php)?', $from); 
    
            $reverse[] = array(
             'from' => $from, 
             'to' => $to 
            ); 
           } 
          } 
         } 
         fclose($file); 
    
         return $reverse; 
        } 
    } 
    
    0

    당신은 두 가지 방법으로이 작업을 수행 할 수 있습니다 ...이 데이터가 저장되는 유일한 장소입니다 리디렉션 루프에 빠지지 않도록주의하십시오.

    비아 htaccess로

    사용한다 RewriteCond은 QUERY_STRING와 redirect depending on the querystring을 확인합니다. Apache가 다른 다시 쓰기 규칙을 계속 실행하지 못하게하려면 [L] 플래그를 추가하고 클라이언트를 리디렉션하려면 R = 301 플래그를 추가하는 것을 잊지 마십시오. PHP는 여기에

    서버에서 클라이언트의 요청과 요청 사이에 구별해야 비아

    . 당신은

    RewriteRule ^home(/)?$ index.php?mod=frontpage&com=showFrontpage&server=1 
    

    그런 다음 코드에서, 매개 변수가 존재하는지 여부를 확인하지 않을 경우 리디렉션 예를 들어, 추가 매개 변수를하여 재 작성 규칙을 변경하고 통과 할 수 있습니다.

    1
    RewriteEngine On 
    RewriteBase/
    
    RewriteCond %{REQUEST_URI} ^/index.php$ 
    RewriteCond %{QUERY_STRING} ^id=(.*)$ 
    RewriteRule ^(.*)$ /home/%1? [R=302] 
    

    example.com/index.php?id=uri은 리디렉션에 : example.com/home/uri

    관련 문제