Jump to:

22989 Posts in 11842 Topics by 2828 members

General Questions

SilverStripe Forums » General Questions » Redirect with header("Location: ...")

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

Page: 1
Go to End
Author Topic: 2294 Views
  • spierala
    Avatar
    Community Member
    80 Posts

    Redirect with header("Location: ...") Link to this post

    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

  • baba-papa
    Avatar
    Community Member
    279 Posts

    Re: Redirect with header("Location: ...") Link to this post

    Have you thought about using a RedirectorPage?

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Redirect with header("Location: ...") Link to this post

    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/');

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: Redirect with header("Location: ...") Link to this post

    > 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.

  • spierala
    Avatar
    Community Member
    80 Posts

    Re: Redirect with header("Location: ...") Link to this post

    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

  • Pigeon
    Avatar
    Community Member
    243 Posts

    Re: Redirect with header("Location: ...") Link to this post

    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.

  • spierala
    Avatar
    Community Member
    80 Posts

    Re: Redirect with header("Location: ...") Link to this post

    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

    2294 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.