Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Redirect with header("Location: ...")


Go to End


7 Posts   4380 Views

Avatar
spierala

Community Member, 80 Posts

3 December 2009 at 1:38am

Edited: 03/12/2009 1:40am

hello all,

i would like to do a redirect to a certain domain. I tried to use the php funtion header("Location: http://www.mydomain.com");

i thought i could edit this in the file main.php at line 63:

if(!headers_sent()) header("Location: http://www.mydomain.com; Content-type: text/html; charset=\"utf-8\"");

but nothing changes.
do you know the right place to change the header location?

greetz,
florian

Avatar
baba-papa

Community Member, 279 Posts

3 December 2009 at 5:15am

Have you thought about using a RedirectorPage?

Avatar
swaiba

Forum Moderator, 1899 Posts

3 December 2009 at 5:46am

Edited: 03/12/2009 5:48am

I've used that many times, but you have to be sure no other output is been made.

reading http://php.net/manual/en/function.headers-sent.php it seems that every example ends with an 'exit' statment to ensure that nothing else is written afterwards. Also I have only used a plain url there have you tried that too?

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

Lastly I agree with above poster, have you tried:

Director::redirect('http://www.example.com/');

Avatar
Hamish

Community Member, 712 Posts

3 December 2009 at 8:53am

Edited: 03/12/2009 8:55am

> i thought i could edit this in the file main.php at line 63:

The very last thing you should do is modify core files like main.php. That just leads to grief.

> it seems that every example ends with an 'exit' statment to ensure that nothing else is written afterwards.

exit(); prevents further code execution, so don't use it after Director::redirect :)

As the other posters have said, use a RedirectorPage, or a method in your controller that uses Director::redirect.

This has the following effect:

1. Sets the correct Location header and HTTP code

2. Provides a suitable html output for browsers that refuse to respect the redirection (or even are just a bit slow about it - it will says "Redirecting to..."

3. Additionally adds meta refresh and javascript redirect just to be sure.

Avatar
spierala

Community Member, 80 Posts

3 December 2009 at 9:56pm

hello together,

i forget to say what i exactly want to do. i have serveral domains linking to the same target / directory. I´ve read that google doesn´t like it to find the same content for different domains. So i want to specify a main domain and the other domains should be redirected to the main domain.

If somebody arrives at otherdomain.com/home he should be redirected to maindomain.com/home.

bye and thanks,
florian

Avatar
dhensby

Community Member, 253 Posts

3 December 2009 at 11:03pm

If you're trying to redirect multiple domains to one, then you really shouldn't use the Location header.

The Location header sends a 302 redirect, not a 301 (what google wants) and it's also hitting PHP for no reason at all.

You can easily set a 301 redirect in the .htaccess or in your servers control panel using domain pointing.

Something like this in your .htaccess should work:

RewriteCond %{QUERY_STRING} ^id=13$
RewriteRule ^/page.php$ http://www.example.com/newname.htm? [L,R=301]

src: http://www.tamingthebeast.net/articles3/spiders-301-redirect.htm

Or in DirectAdmin control panel you can set domain pointers so that lots of domains point to one domain - just add a pointer and uncheck the 'create alias' box. I'm not sure about cPanel or others, but i'm sure it is possible.

Avatar
spierala

Community Member, 80 Posts

26 December 2009 at 6:04am

hello,

redirecting via htaccess worked fine. for each domain i added these two lines at the top of the htaccess file:

RewriteCond %{HTTP_Host} ^(www\.)?otherdomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.maindomain.com/$1 [L,R=301]

greetz,
flo