php 使用 CodeIgniter 重定向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/723883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Redirect with CodeIgniter
提问by ocergynohtna
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.mysite.com/index/provider1/when it should route to www.mysite.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? Thanks in advance!
谁能告诉我为什么我的重定向助手不能像我期望的那样工作?我正在尝试重定向到我的主控制器的 index 方法,但是www.mysite.com/index/provider1/当它应该路由到www.mysite.com/provider1. 这对任何人都有意义吗?我将配置中的索引页设置为空白,尽管我认为这不是问题所在。有没有人有关于如何解决这个问题的建议?提前致谢!
Controller:
控制器:
if($provider == '') {
redirect('/index/provider1/', 'location');
}
.htaccess:
.htaccess:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^(index\.php|files|images|js|css|robots\.txt|favicon\.ico)
RewriteCond %{HTTP_HOST} ^mysite.com/ttnf/
RewriteRule (.*) http://www.mysite.com/ttnf/ [R=301,L]
RewriteBase /ttnf/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/ [L]
php_flag display_errors On
回答by Jon Winstanley
redirect()
重定向()
URL Helper
网址助手
代码点火器中的重定向语句使用重定向头语句将用户发送到指定的网页。
This statement resides in the URL helper which is loaded in the following way:
该语句驻留在 URL 帮助器中,它以以下方式加载:
$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.
重定向函数加载在函数调用的第一个参数中指定的本地 URI,并使用配置文件中指定的选项构建。
The second parameter allows the developer to use different HTTP commands to perform the redirect "location" or "refresh".
第二个参数允许开发人员使用不同的 HTTP 命令来执行重定向“位置”或“刷新”。
According to the Code Igniter documentation: "Location is faster, but on Windows servers it can sometimes be a problem."
根据 Code Igniter 文档:“定位速度更快,但在 Windows 服务器上有时可能会出现问题。”
Example:
例子:
if ($user_logged_in === FALSE)
{
redirect('/account/login', 'refresh');
}
回答by Siddharth Shukla
first, you need to load URL helper like this type or you can upload within autoload.php file:
首先,你需要像这样加载 URL helper 或者你可以在 autoload.php 文件中上传:
$this->load->helper('url');
if (!$user_logged_in)
{
redirect('/account/login', 'refresh');
}
回答by Raham
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');
}
回答by Md Safiul Alam
If you want to redirect previous location or last request then you have to include user_agentlibrary:
如果要重定向上一个位置或上次请求,则必须包含user_agent库:
$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.
它为我工作。
回答by Alpesh-Prajapati
Here is .htacess file that hide index file
这是隐藏索引文件的 .htacess 文件
#RewriteEngine on
#RewriteCond !^(index\.php|images|robots\.txt)
#RewriteRule ^(.*)$ /index.php/ [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/*(.*) / [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/ [L]
</IfModule>

