CodeIgniter Forums
Redirect to absolute URL - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Redirect to absolute URL (/showthread.php?tid=13866)



Redirect to absolute URL - El Forum - 12-09-2008

[eluser]maesk[/eluser]
Hi,

the URL Helper's redirect() function does a header redirect to a local URI, that is a relative URL. Sometimes, however, I'll have to redirect to an absolute URL, for example when I have an optional number of parameters for my controller. Should I just use

Code:
header('Location: http://www.example.com/');

for these cases or is there a CI specific method to do this?


Redirect to absolute URL - El Forum - 12-09-2008

[eluser]OES[/eluser]
Just redirect(''); will do the trick.

Good Luck !


Redirect to absolute URL - El Forum - 12-09-2008

[eluser]maesk[/eluser]
That's one reason why I love CodeIgniter: you have a question, you get an answer in 5 minutes on the forums... awesome!
However, I probably didn't explain properly what I wanted to achieve. Your solution would just redirect me to my homepage (the base_url), but that's not what I want.

Let's say I want to be redirected to

Code:
http://www.mypage.com/controller1/function1

when I am at

Code:
http://www.mypage.com/controller2/function1/

I can't just do a

Code:
redirect('controller1/function1');

because this would take me to the URL http://www.mypage.com/controller2/function1/controller1/function1

If I use

Code:
redirect(site_url('controller1/function1'));

it would take me to http://www.mypage.com/controller2/function1/http://www.mypage.com/controller1/function1

It would be useful if the redirect() function could be used relative AND absolute, for example when it would use an absolute URL as soon as site_url() is included.
But I guess I have to use something like custom routes for this.

Edit: some of you are probably going to suggest to simply remap controller1/function1 to controller2/function1 but I want to redirect only under certain conditions, such as when there are no params in the URL but POST data exists.


Redirect to absolute URL - El Forum - 12-09-2008

[eluser]OES[/eluser]
Hi

you can redirect to anywhere so like you have said add a forward slah to make it a root redirect ie.

redirect('/controller2/function2/');

Regards

Lee


Redirect to absolute URL - El Forum - 12-09-2008

[eluser]maesk[/eluser]
Cool, it works!! This was much easier than I thought - just add the slash at the beginning... I wouldn't have tought of that (duh!).
Thanks a lot!
maesk