2015-03-13 50 views
1

我想www.siteurl.com/user/username而不是www.siteurl.com/user?name=username來獲取用戶的個人資料自定義配置文件的URL像<site url>/<username>笨

這完全是在localhost中工作,但不在活服務器 它只是給404未找到頁面,但在本地主機它指向請求的網址。我怎樣才能在現場服務器做到這一點?

這是我的代碼

config.php啓用鉤

$config['enable_hooks'] = TRUE; 

然後創建一個hooks.php加入以下在應用程序 - >配置

$hook=array(
     'pre_system' => array(
       array(
         'class' => 'Userlookup', 
         'function' => 'check_uri', 
         'filename' => 'Userlookup.php', 
         'filepath' => 'hooks', 
         'params' => NULL, 
       ), 
     ), 
); 

代碼然後,在應用程序 - >掛鉤創建它包含的Userlookup.php,

<?php defined('BASEPATH') or die('No direct script access allowed'); 

class Userlookup{ 

    protected $uri_username; 
    protected $connection_method; 

    protected $hostname; 
    protected $username; 
    protected $password; 
    protected $database; 

    public function __construct() 
    { 
     // Configure database connection 
     include(APPPATH.'config/database'.EXT); 
     $this->hostname = $db[$active_group]['hostname']; 
     $this->username = $db[$active_group]['username']; 
     $this->password = $db[$active_group]['password']; 
     $this->database = $db[$active_group]['database']; 

    } 

    public function check_uri() 
    { 
     // First, we need get the uri segment to inspect 
     $request_uri = explode('/',substr($_SERVER['REQUEST_URI'], 1)); //$request_uri[0] == username 
     $this->uri_username = array_shift($request_uri);     //string 'socialsite' (length=10) 
     $connection_router = array_shift($request_uri); 
     $this->connection_method = empty($connection_router) ? 'index' : $connection_router; 
     // Connect to database, and check the user table 
     mysql_connect($this->hostname, $this->username, $this->password) AND mysql_select_db($this->database); 
     $res = mysql_query("SELECT user_Id FROM tbl_user WHERE user_name='".$request_uri[0]."'"); 
     // $id = mysql_result("SELECT user_Id FROM tbl_user WHERE user_name='".$request_uri[0]."'"); 
     // var_dump($id,0);die; 

     // print_r($id);die; 

     if ($row = mysql_fetch_object($res)) 
     { 
       // If, there is a result, then we should modify server data 
       // Below line means, we told CodeIgniter to load 
       // 'User' controller on 'index', 'info' or any valid connection/method and we send 'id' parameter 
       //$_SERVER['REQUEST_URI'] = '/user'; 
      $id = mysql_result($res,0); 
       //print_r($id);die; 
       $_SERVER['REQUEST_URI'] = '/user?id='.$id; 
       //var_dump($_SERVER['REQUEST_URI']);die; 

     } 
     mysql_free_result($res); 
    } 

} 

這是我的.htaccess文件

RewriteEngine On 
RewriteCond %{REQUEST_URI} ^system.* 
RewriteRule ^(.*)$ /index.php/$1 [L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 

RewriteRule ^(.*)$ index.php?/$1 [L,QSA] 
+0

您是否在共享主辦?你可以http://gotoanswer.stanford.edu/?q=Codeigniter+redirect+not+working檢查那裏的一些,也如果你可以讀取服務器錯誤日誌,它也會幫助你 – 2015-03-13 07:12:16

+0

你是什麼意思'不工作'請指明任何錯誤或其行爲。 – 2015-03-13 09:38:16

+0

它給404錯誤「您請求的頁面未找到。」 – Krish 2015-03-13 09:51:53

回答

1

最後,我創建了URL,因爲我想如example.com/username。在這裏,我將它作爲其他人的幫助發佈。

For more

我用標準的MVC

這種類型的個性化網址涉及URI路由,所以添加以下代碼爲貴 「的application/config/routes.php文件」

if($handle = opendir(APPPATH.'/controllers')) 
{ 
    while(false !== ($controller = readdir($handle))) 
     { 
      if($controller != '.' && $controller != '..' && strstr($controller, '.') == '.php') 
       { 
        $route[strstr($controller, '.', true)] = strstr($controller, '.', true); 
        $route[strstr($controller, '.', true).'/(:any)'] = strstr($controller, '.', true).'/$1'; 
       } 
     } 
    closedir($handle); 
} 

// these are the /username routes 
$route['([a-zA-Z0-9_-]+)'] = 'controller/index/$1'; 

然後在你的控制器寫你可以從GET獲得用戶名。

function index($username = '') 
{ 
    echo $username; 
    //your code here 
} 
0

對於你所要求的東西,你不需要一個鉤子,也沒有一個htaccess或事件路由文件搞亂。

由於您在url中保留了控制器名稱/ users/[username]所有您需要的是定義Codeigniter提供的catch-all方法。

你需要像這樣在你的「Users.php」控制器:

public function _remap ($method, $params = array()){ 
    if(method_exists($this, $method)) { 
     return call_user_func_array(array($this, $method), $params); 
    }else{ 
     $Username = $method; 
     $data['User'] = $this->users_model->get_by_username($Username); 
     $this->load->view('users/profile', $data); 
    } 
}