使用CodeIgniter重定向

b4lqfgs4  于 2022-12-20  发布在  其他
关注(0)|答案(5)|浏览(142)

有人能告诉我为什么我的重定向助手不能像我期望的那样工作吗?
我试图重定向到我的主控制器的索引方法,但它需要我www.example.com/index/provider1/时,它应该路由到www.example.com/provider1 .这是有意义的人吗?我有配置中的索引页设置为空白,虽然我不认为这是问题.
有人对如何解决这个问题有什么建议吗?

控制器

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
tkclm6bt

tkclm6bt1#

重定向()

URL帮助程序

code igniter中的redirect语句使用redirect header语句将用户发送到指定的网页。
此语句驻留在URL帮助器中,该帮助器通过以下方式加载:

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

redirect函数加载在函数调用的第一个参数中指定并使用配置文件中指定的选项构建的本地URI。
第二个参数允许开发人员使用不同的HTTP命令来执行重定向“定位”或“刷新”。
根据代码点火器文件:定位速度更快,但在Windows服务器上有时会出现问题。
示例:

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

vlurs2pr2#

如果目录结构是这样的,

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');
}
y4ekin9u

y4ekin9u3#

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

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

最后用在你正在使用的函数中

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

这对我很有效。

smdnsysy

smdnsysy4#

首先,你需要加载这样的URL助手,或者你可以在autoload.php文件中上传:

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

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

xa9qqrwz5#

下面是隐藏索引文件的.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>

相关问题