Redirect with CodeIgniter
Asked Answered
P

5

69

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
Polito answered 7/4, 2009 at 1:39 Comment(2)
DUH.... i'm an idiot today, i wasn't sending it to my controller (which is main.php) instead it was as if i was trying to load a controller named index. so sorry.Polito
This is a very useful CodeIgniter question. The interaction between Apache rules and CI routes needs more examples on the internet.Heteronomy
A
159

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');
}
Amedeo answered 7/4, 2009 at 11:27 Comment(0)
F
5

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');
}
Frisch answered 20/12, 2014 at 9:45 Comment(0)
A
5

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.

Astern answered 28/9, 2016 at 7:19 Comment(0)
A
5

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');
}
Antonioantonius answered 20/7, 2017 at 21:2 Comment(0)
M
1

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>
Mohl answered 28/9, 2016 at 8:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.