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.

General Questions

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

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

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


Reply

7 Posts   2546 Views

Avatar
spierala

3 December 2009 at 1:38am (Last edited: 3 December 2009 1:40am), Community Member, 80 Posts

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

3 December 2009 at 5:15am Community Member, 279 Posts

Have you thought about using a RedirectorPage?

Avatar
swaiba

3 December 2009 at 5:46am (Last edited: 3 December 2009 5:48am), Forum Moderator, 1796 Posts

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

reading [url]http://php.net/manual/en/function.headers-sent.php[/url] 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

3 December 2009 at 8:53am (Last edited: 3 December 2009 8:55am), Community Member, 712 Posts

> 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

3 December 2009 at 9:56pm Community Member, 80 Posts

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
Pigeon

3 December 2009 at 11:03pm Community Member, 243 Posts

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

26 December 2009 at 6:04am Community Member, 80 Posts

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