doulou0882 2015-03-13 06:29
浏览 36
已采纳

自定义配置文件URL,如<site url> / <username> Codeigniter

I want to get the user's profile by www.siteurl.com/user/username instead of www.siteurl.com/user?name=username

This is perfectly working in localhost, but not in live server It just give a 404 Page Not Found, But in localhost it directs to requested url. How can I do this in live server?

This is my code

In config.php enabled the hooks

$config['enable_hooks'] = TRUE; 

Then create a hooks.php adding following code in application->config

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

Then, in application-> hooks create Userlookup.php it contains,

<?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);
    }

} 

This is my .htaccess file

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]
  • 写回答

2条回答 默认 最新

  • doulu1325 2015-03-26 11:41
    关注

    Finally, I have created the URL as I wanted such as example.com/username. Here I am posting it as a help for others.

    For more

    I used standard MVC

    This type of vanity URL involve URI routing, So add below code for your "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';
    

    Then in your controller write you can get the username from GET.

    function index($username = '')
    {
        echo $username;
        //your code here
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测