2013-03-23 39 views
1

好的,我認爲eveything是hunky-dory,直到我想縮短几個URL,我認爲這將是一塊蛋糕。又一個Htaccess問題

我已經開發了自己的MVC結構,其中所有頁面都是通過index.php進行的,並且取決於url,相關的控制器和模型會隨即加載。這給了我很好和乾淨的網址,並使應用程序更容易開發,因爲它變得更大。

所以我的.htaccess文件中,我有以下:

RewriteRule ^(.+)$ index.php?url=$1 [QSA,L] 

和我的index.php內我指望在需要的控制器等的URL和負載的長度...

$length = count($url); 

if($length > 5) 
     { 
      $this->Error->index(); 
      return FALSE; 
     } 

    switch ($length) 
    { 
     case 5: 
      $controller_name->{$url[1]}($url[2], $url[3], $url[4]); 
      break; 

     case 4: 
      $controller_name->{$url[1]}($url[2], $url[3]); 
      break; 

     case 3: 

      $controller_name->{$url[1]}($url[2]); 
      break; 

     case 2: 
      $controller_name->{$url[1]}(); 
      break; 

     default: 
      $controller_name->index(); 
      break; 
    } 

這一切工作絕對好,所以這裏是我的問題,我有一個網址www.thepage.com/accounts/general_users/profile/[account_name]

所以我想縮短這個網址通過切掉賬戶/ general_users /「我累了我的htaccess中的以下內容:

RewriteRule ^profile/april.lee$  accounts/general_users/profile/april.lee [QSA,L] 

但這似乎並不想發揮好與我最初的重寫規則,如果它的第一個重寫規則之下的地方它只是不渲染,如果它的上面它似乎打破申請。

我很難在做什麼,並會喜歡一點指導/建議。我真的想拿出一個解決方案,而不必重新工作整個應用程序。

更新來證明我是如何加載我的控制器和模型:

1 - 我得到和清潔的網址:

// Get Clean URL 
$url = isset($_GET['url']) ? $_GET['url'] : NULL; 
$url = rtrim($url, '/'); 
$url = filter_var($url, FILTER_SANITIZE_URL); 
$url = explode('/', $url); 

// Start Session 
session_start(); 

// Create Application Core 
require_once(APP_PATH . "core/core.php"); 
$AC = new Application_Core($config); 

2-檢查它是否是空的或不

if(empty($url[0])) 
{ 
     // Load and Instantiate Model 
    require(APP_PATH."models/m_index.php"); 
    $this->Model = new m_index(); 

    // Load and Instantiate Controller 
    require(APP_PATH."controllers/index.php"); 
    $controller = new index(); 
    $controller->index(); 
} 

else 
{ 

// Load and Instantiate Model 
     $model_file = APP_PATH."models/m_".$url[0].".php"; 
     if(file_exists($model_file)) 
     { 
      include($model_file); 
      $model_name = "m_".$url[0]; 
      $this->Model = new $model_name(); 
     } 
     else 
     { 
      echo $url[0]; 
      $this->Template->render_error($this->error_page, $this->error_message); 
      return FALSE; 
     } 

     // Load and Instantiate Controller 
     $controller_file = APP_PATH."controllers/".$url[0].".php"; 

     if(file_exists($controller_file)) 
     { 
      require(APP_PATH."controllers/".$url[0].".php"); 
      $controller_name = $url[0]; 
      $controller_name = new $controller_name(); 
     } 
     else 
     { 
      $this->Template->render_error($this->error_page, $this->error_message); 
      return FALSE; 
     } 

     $length = count($url); 

     // Check Method Exists 
     if ($length > 1) { 
      if (!method_exists($controller_name, $url[1])) { 
       $this->Template->render_error($this->error_page, $this->error_message); 
       return FALSE; 
      } 
     } 

     if($length > 5) 
     { 
      $this->Error->index(); 
      return FALSE; 
     } 

     switch ($length) 
     { 
      case 5: 
       $controller_name->{$url[1]}($url[2], $url[3], $url[4]); 
       break; 

      case 4: 
       $controller_name->{$url[1]}($url[2], $url[3]); 
       break; 

      case 3: 

       $controller_name->{$url[1]}($url[2]); 
       break; 

      case 2: 
       $controller_name->{$url[1]}(); 
       break; 

      default: 
       $controller_name->index(); 
       break; 


     } 

3 - 檢查長度並加載適當的控制器和模型,如果方法不存在,則用戶重定向到錯誤頁面。

+0

重寫規則^輪廓/(.*)$賬戶/ general_users /資料/ $ 1 [QSA,L] – Adam 2013-03-23 17:52:10

+0

嗨@Adam感謝您的評論,但似乎造就了可怕的500內部服務器錯誤頁面。 – HireLee 2013-03-23 17:58:03

+0

我補充說明我的意思是一個更好的解答。 – Adam 2013-03-23 18:02:29

回答

0

這看起來好像可以通過不控制URL長度來更好地處理它。如果有人在最​​短的鏈接末尾輸入額外的變量以查看會發生什麼,會發生什麼?他們是否會從您的網站獲得針對其他用戶的行爲,這可能很糟糕。

您可以使用htaccess以較少的頭痛來完成您的交換機正在執行的任何操作。

RewriteCond %{http_host} ^mysite.com/ [nc] 
RewriteRule ^(.*)$ http://www.mysite.com/$1 [nc] 

RewriteRule ^profile/(.*)$ accounts/general_users/profile/$1 [QSA, L] 
RewriteRule ^url_for_control1/match$ index.php?controller=1&%{QUERY_STRING} 

編輯:
爲了讓您的index.php文件這項工作要看它做什麼,如果你的控制功能,將用戶重定向到另一個頁面,您可以讓htaccess的爲你做的。

例如,以下..

RewriteCond %{http_host} ^mysite.com/ [nc] 
RewriteRule ^(.*)$ http://www.mysite.com/$1 [nc] 

RewriteRule ^employees/(.*)/(.*)$ index.php?first=$1&last=$2&%{QUERY_STRING} 

會重定向請求:
http://www.mysite.com/employees/john/doe

到:
http://www.mysite.com/index.php?first=john&last=doe&anyother=variables_sent

...使用取決於如何請求頁面GET或POST變量。

瞭解這一點,您可以創建更清晰的網址,搜索引擎友好且易於人類閱讀和理解。您可以通過在url中搜索特定的特徵並通過要使用的控制函數的變量告訴index.php頁面,繼續向index.php發送請求。

這不是一個好主意,決定只對字符的URL中的原因是多方面的,其中包括製造,可以輕易地改變和升級的站點數量基於頁面的操作。

Net Tuts has a decent beginners tutorial as well for htaccess.

+0

嗨,亞當,這看起來有點令人困惑,因爲我不太熟悉htaccess,我更新了我的問題,以顯示index.php文件的內部運作...我將如何去實現上面的代碼來處理我的索引。 PHP ...林有點困惑...乾杯李 – HireLee 2013-03-23 18:23:43

+0

@LeeMarshall我擴大了一點,謝謝。 – Adam 2013-03-23 22:51:31