views:

317

answers:

3

I noticed that anchor('controller/method') produces a different result than a mere <a href="controller/method"> in that anchor() adds the base_url:

anchor('controller/method'):

<a href="http://localhost/dts/controller/method"&gt;Link&lt;/a&gt;

<a>:

<a href="controller/method">Link</a>

How do I achieve this same effect (anchor) in my controller? That is, adding the base_url in my redirects?

I'm asking because I have a form that calls another method method2, which has a redirect('controller/method'). But it redirects incorrectly to controller/controller/method2, then if the form is submitted again, redirects to controller/controller/controller/method2 and so on.

That's why I'd like to know how to redirect to controller/method with the base_url pre-prended to it. redirect( base_url() . 'controller/method' ) doesn't work.

index_page is set to:

$config['index_page'] = "";

base_url is set to:

$config['base_url'] = "http://localhost/program/";

.htaccess:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule (.*) index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
    ErrorDocument 404 /index.php
</IfModule>

that's based on this because the .htaccess provided by the CI manual doesn't work.

Any ideas?

A: 
redirect('/controller/method');
kemp
this redirects to http://localhost/controller/method instead of http://localhost/program/controller/method
Obay
What is "program"?
kemp
lol that's the parent folder of CI's system folder
Obay
cos my system folder is found under htdocs/program/system
Obay
well, 'program' is the name of my web app
Obay
i updated the base_url and index_page values above, i incorrectly copied it before.
Obay
Well, setting the program name in `base_url` and using the syntax in my answer works fine for me: it redirects to `http://host/program_dir/controller/method`
kemp
A: 

Almost kemp, but no slashes needed.

redirect('controller/method');

What is your $config['base_url'] set to, because you are using the right functions and getting the wrong functionality.

Phil Sturgeon
I thought what the OP is getting was the expected behavior since he's providing a relative path.
kemp
dude i added the base_url info above
Obay
i updated the base_url and index_page values above, i incorrectly copied it before.
Obay
kemp: The slashes in the redirect() just screw with things, it has no effect on relative/absolute. Obay: How are you submitting your forms? if you are not using form_open('controller/method') it could be causing a problem.
Phil Sturgeon
im manually doing <form method="post" action="controller/method" />, haha i should try form_open()
Obay
<form method="post" action="<?php echo site_url('controller/method');?>">
Phil Sturgeon
i used form_open('controller/method') and it works! thanks man
Obay
A: 

I think you want redirect( site_url('controller/method') );

Don't forget to add the URL Helper class before using this $this->load->helper('url');.

Kurucu