开发者

CodeIgniter htaccess Redirect

开发者 https://www.devze.com 2022-12-24 17:31 出处:网络
I\'m hoping that this will be a simple question that someone can answer. I\'m looking to build a CodeIgniter application that I can build pretty easily in regular PHP.

I'm hoping that this will be a simple question that someone can answer. I'm looking to build a CodeIgniter application that I can build pretty easily in regular PHP.

Example: I would like to go to http://locahost/gregavola and have rewritten using htaccess file to profile.php?user=gregavola. How can I do this in CodeIgniter?

Usually in htaccess I could wr开发者_Go百科ite ^(\w+)$ profile.php?user=$1 but that won't work with the paths in CodeIgniter.

Any suggestions?


CodeIgniter turns off GET parameters by default; instead of rewriting the URL to a traditional GET style (IE, with the ?), you should create a user controller and send the request to:

http://localhost/user/info/gregavola

Then in the user controller, add the following stub:

function info($name)
{
    echo $name;
}

From here you would probably want to create a view and pass $name into it:

  $data['name'] = 'Your title';
  $this->load->view('user_info', $data);

You can find all of this in the CodeIgniter User Guide, which is an excellent resource for getting started.


To map localhost/gregavola to a given controller and function, modify the routes file at application/config/routes.php like so:

$route['(:any)'] = "user/info/$1"

Routes are run in the order they are received, so if you have other routes like localhost/application/dosomething/, you will want to include those routes first so that every page in your entire app doesn't become a user page.

Read more about CI routes here: http://codeigniter.com/user_guide/general/routing.html

Good luck!

0

精彩评论

暂无评论...
验证码 换一张
取 消