CodeIgniter htaccess 重定向

发布于 2024-08-27 02:56:08 字数 361 浏览 6 评论 0 原文

我希望这将是一个有人可以回答的简单问题。我正在寻找构建一个 CodeIgniter 应用程序,我可以在常规 PHP 中轻松构建它。

示例:我想访问 http://locahost/gregavola 并使用 htaccess 文件重写为 profile.php?用户=gregavola。我怎样才能在 CodeIgniter 中做到这一点?

通常在 htaccess 中我可以编写 ^(\w+)$ profile.php?user=$1 但这不适用于 CodeIgniter 中的路径。

有什么建议吗?

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 write ^(\w+)$ profile.php?user=$1 but that won't work with the paths in CodeIgniter.

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怎言笑 2024-09-03 02:56:08

CodeIgniter默认关闭GET参数;您应该创建一个用户控制器并将请求发送到:

http://localhost/user/info/gregavola

然后在 user控制器,添加以下存根:

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

从这里您可能想要创建一个视图并将 $name 传递到其中:

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

您可以在 CodeIgniter 用户指南,这是一个很好的入门资源。

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.

拥抱影子 2024-09-03 02:56:08

要将 localhost/gregavola 映射到给定的控制器和函数,请修改 application/config/routes.php 中的路由文件,如下所示:

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

路由按照收到的顺序运行,因此如果您有其他路由像 localhost/application/dosomething/ 一样,您需要首先包含这些路由,以便整个应用程序中的每个页面都不会成为用户页面。

在这里阅读有关 CI 路由的更多信息: http://codeigniter.com/user_guide/general/routing.html祝

你好运!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文