使用 CodeIgniter 重定向

发布于 2024-07-16 17:05:59 字数 783 浏览 10 评论 0原文

谁能告诉我为什么我的重定向助手不能按我期望的方式工作?

我正在尝试重定向到主控制器的索引方法,但当它应该路由到 www.example.com/ 时,它需要我 www.example.com/index/provider1/提供商1。 这对任何人都有意义吗? 我将配置中的索引页面设置为空白,尽管我不认为这是问题所在。

有人对如何解决这个问题有建议吗?

控制器

if($provider == '') {
    redirect('/index/provider1/', 'location');
}

.htaccess

RewriteEngine on

RewriteCond %{REQUEST_URI} !^(index\.php|files|images|js|css|robots\.txt|favicon\.ico)

RewriteCond %{HTTP_HOST} ^example.com/ttnf/
RewriteRule (.*) http://www.example.com/ttnf/$1 [R=301,L]

RewriteBase /ttnf/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

php_flag display_errors On

Can anyone tell me why my redirect helper does not work the way I'd expect it to?

I'm trying to redirect to the index method of my main controller, but it takes me www.example.com/index/provider1/ when it should route to www.example.com/provider1. Does this make sense to anyone? I have index page in config set to blank, although I don't think that it is the issue.

Does anyone have advice on how to fix this issue?

Controller:

if($provider == '') {
    redirect('/index/provider1/', 'location');
}

.htaccess:

RewriteEngine on

RewriteCond %{REQUEST_URI} !^(index\.php|files|images|js|css|robots\.txt|favicon\.ico)

RewriteCond %{HTTP_HOST} ^example.com/ttnf/
RewriteRule (.*) http://www.example.com/ttnf/$1 [R=301,L]

RewriteBase /ttnf/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

php_flag display_errors On

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

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

发布评论

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

评论(5

酒儿 2024-07-23 17:05:59

redirect()

URL 帮助程序


The redirect statement in code igniter sends the user to the specified web page using a redirect header statement.

此语句驻留在 URL 帮助程序中,该帮助程序按以下方式加载:

$this->load->helper('url');

重定向函数加载函数调用的第一个参数中指定的本地 URI,并使用配置文件中指定的选项构建。

第二个参数允许开发者使用不同的HTTP命令来执行重定向“定位”或“刷新”。

根据 Code Igniter 文档:“定位速度更快,但在 Windows 服务器上有时可能会成为问题。”

例子:

if ($user_logged_in === FALSE)
{
     redirect('/account/login', 'refresh');
}

redirect()

URL Helper


The redirect statement in code igniter sends the user to the specified web page using a redirect header statement.

This statement resides in the URL helper which is loaded in the following way:

$this->load->helper('url');

The redirect function loads a local URI specified in the first parameter of the function call and built using the options specified in your config file.

The second parameter allows the developer to use different HTTP commands to perform the redirect "location" or "refresh".

According to the Code Igniter documentation: "Location is faster, but on Windows servers it can sometimes be a problem."

Example:

if ($user_logged_in === FALSE)
{
     redirect('/account/login', 'refresh');
}
两仪 2024-07-23 17:05:59

如果您的目录结构是这样的,

site
  application
         controller
                folder_1
                   first_controller.php
                   second_controller.php
                folder_2
                   first_controller.php
                   second_controller.php

并且当您要在您正在工作的同一控制器中重定向它时,只需编写以下代码即可。

 $this->load->helper('url');
    if ($some_value === FALSE/TRUE) //You may give 0/1 as well,its up to your logic
    {
         redirect('same_controller/method', 'refresh');
    }

如果您想重定向到另一个控件,请使用以下代码。

$this->load->helper('url');
if ($some_value === FALSE/TRUE) //You may give 0/1 as well,its up to your logic
{
     redirect('folder_name/any_controller_name/method', 'refresh');
}

If your directory structure is like this,

site
  application
         controller
                folder_1
                   first_controller.php
                   second_controller.php
                folder_2
                   first_controller.php
                   second_controller.php

And when you are going to redirect it in same controller in which you are working then just write the following code.

 $this->load->helper('url');
    if ($some_value === FALSE/TRUE) //You may give 0/1 as well,its up to your logic
    {
         redirect('same_controller/method', 'refresh');
    }

And if you want to redirect to another control then use the following code.

$this->load->helper('url');
if ($some_value === FALSE/TRUE) //You may give 0/1 as well,its up to your logic
{
     redirect('folder_name/any_controller_name/method', 'refresh');
}
瑶笙 2024-07-23 17:05:59

如果您想重定向以前的位置或最后一个请求,那么您必须包含 user_agent 库:

$this->load->library('user_agent');

然后在您正在使用的函数中使用最后:

redirect($this->agent->referrer());

它对我有用。

If you want to redirect previous location or last request then you have to include user_agent library:

$this->load->library('user_agent');

and then use at last in a function that you are using:

redirect($this->agent->referrer());

its working for me.

楠木可依 2024-07-23 17:05:59

首先,您需要加载此类类型的 URL 帮助程序,或者您可以上传
在 autoload.php 文件中:

$this->load->helper('url');

if (!$user_logged_in)
{
  redirect('/account/login', 'refresh');
}

first, you need to load URL helper like this type or you can upload
within autoload.php file:

$this->load->helper('url');

if (!$user_logged_in)
{
  redirect('/account/login', 'refresh');
}
仄言 2024-07-23 17:05:59

这是隐藏索引文件的.htacess 文件

#RewriteEngine on
#RewriteCond $1 !^(index\.php|images|robots\.txt)
#RewriteRule ^(.*)$ /index.php/$1 [L]

<IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /

        # Removes index.php from ExpressionEngine URLs
        RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
        RewriteCond %{REQUEST_URI} !/system/.* [NC]
        RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

        # Directs all EE web requests through the site index file
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

Here is .htacess file that hide index file

#RewriteEngine on
#RewriteCond $1 !^(index\.php|images|robots\.txt)
#RewriteRule ^(.*)$ /index.php/$1 [L]

<IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /

        # Removes index.php from ExpressionEngine URLs
        RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
        RewriteCond %{REQUEST_URI} !/system/.* [NC]
        RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

        # Directs all EE web requests through the site index file
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文