Setting up redirects in PHP

Discussion in 'PHP' started by sachamps, May 11, 2010.

  1. #1
    I've recently split my site into two, and moved one section to a new domain. So, I have a situation where I need to have users redirected from:

    www.olddomain.com/blog to www.newdomain.com/

    Obviously, I need to keep my page/directory structure intact - so if the user was on www.olddomain.com/blog/category/post555 they need to end up on www.newdomain/category/post555 - you'll notice that the site has moved from a sub-directory to the root of the domain, which adds an extra dimension to my problem.

    I'm hosted on a Windows shared server, so I don't have the ability to use .htaccess - I HAVE to find out if there is a way to do it via PHP headers - if there isn't a way to do it, that's fine - but if it is possible, please help me out here :) I should also point out that I'm a complete PHP noob, so explain it as simply as possible.
     
    sachamps, May 11, 2010 IP
  2. TheRien

    TheRien Peon

    Messages:
    92
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I think that's possible...
    put the following at start of your page, that means, at the very very start
    so put this bit of php code even BEFORE THE DOCTYPE
    I'm not 100% sure if it works though, but I hope it does :p
    just replace blog with the correct directory and newdomain.com with your new domain ;)
    for thank you message and fan mail: :p

    <?php
    $newurl=$_SERVER['PHP_SELF'];
    $newurl=str_ireplace("blog","",$newurl);
    $newurl="newdomain.com".$newurl;
    header('Location: '.$newurl);
    ?>
     
    TheRien, May 11, 2010 IP
  3. gapz101

    gapz101 Well-Known Member

    Messages:
    524
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    150
    #3
    mod_rewrite then
     
    gapz101, May 11, 2010 IP
  4. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If the blog has been working with URIs like that then they must have been run through a URL rewriter. Being on windows doesn't restrict the use of .htaccess files, so long as the server is running Apache for the HTTP requests. Do you know what HTTP service is running?
     
    JAY6390, May 11, 2010 IP
  5. gapz101

    gapz101 Well-Known Member

    Messages:
    524
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    150
    #5
    yah or you may have to change domain/path in your wp settings.. (that is if you're using wp)
     
    gapz101, May 11, 2010 IP
  6. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #6
    This will work :)

     
    roopajyothi, May 11, 2010 IP
  7. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #7
    That will work IF you could redirect to the file, otherwise you'd have to create a file for each and every URI in the blog!
     
    JAY6390, May 11, 2010 IP
  8. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #8
    Dude placing this scrip above <?php in the index file makes the job simpler

    <?php
    header("HTTP/1.1 301 Moved Permanently");
    $request_url =$_SERVER['REQUEST_URI'];
    $via_url= str_ireplace("blog","",$request_url);
    $redirect_new_url = "http://newsite.com".$via_url;
    header("Location: $redirect_new_url");
    ?> 
    PHP:

    Thanks
     
    roopajyothi, May 11, 2010 IP
  9. sachamps

    sachamps Peon

    Messages:
    56
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thanks for the replies guys - I'll play around with the suggestions and see what works.

    With regards to the HTTP service question - I have no idea. I'm on a shared server and I don't have admin access - unless you can suggest a way for me to check.

    I've tried testing an .htaccess - and all I've been able to confirm is that it doesn't work. So it looks like mod_rewrite isn't an option.
     
    sachamps, May 12, 2010 IP
  10. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #10
    @roopajyothi - I'm perfectly aware of how bootstrapping/routing through an index.php file works. That isn't the issue. The problem is that you need to rewrite the urls to the file. Once they're routed to the file, yes your code will work, however you are missing the fundamental flaw in your process, without rewriting, every URL won't run through the index.php file
     
    JAY6390, May 12, 2010 IP
  11. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #11
    JAY6390, May 12, 2010 IP
  12. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #12
    danx10, May 12, 2010 IP
  13. abstractworld

    abstractworld Peon

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    url rewriting using htaccess will be best solution...
    using php isn't the best way... it will work fine only when each and every page runs through index.
     
    abstractworld, May 12, 2010 IP
  14. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #14
    Exactly and mostly in WP is said a Virtual Static but Dynamic in nature
     
    roopajyothi, May 13, 2010 IP
  15. swashata

    swashata Member

    Messages:
    86
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #15
    swashata, May 13, 2010 IP
  16. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #16
    That's OP's Wish since he splitted his site into single site into two :)
     
    roopajyothi, May 14, 2010 IP