Redirect a website with html or php code

Discussion in 'PHP' started by ian_ok, Feb 14, 2006.

  1. #1
    I've been trying along with NINTENDO to use htaccess to redirect a dynamic php file to another page, but without success.

    Can add I add any code to the page that when users or SE's visit this page they are redirected to say my homepage in way similar to a 301 so not to lose any reputation with SE?

    The page isn't needed anymore and if I just remove it I'll get lots of 404 errors, lost visitors and Google has about 500 pages of this page.

    Thanks Ian
     
    ian_ok, Feb 14, 2006 IP
  2. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    header("HTTP/1.1 301 Moved Permanently"); 
    header("Location: http://www.domain.com"); 
    exit();
    
    PHP:
     
    mad4, Feb 14, 2006 IP
  3. ian_ok

    ian_ok Peon

    Messages:
    551
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I get an error:
    Warning: Cannot modify header information - headers already sent by (output started at /blah/blah/blah/folder/folder2/file.php:1318) in /blah/blah/blah/folder/folder2/folder3/folder4/file_top1.html on line 1
    Code (markup):
    I placed this in file_top1.html, but this is a template that is called for the dynamic page. How do I get round this?

    Ian
     
    ian_ok, Feb 14, 2006 IP
  4. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The code I posted needs to be the first thing sent to the browser. If you have a template you may need to put it in the file that calls the template instead.
     
    mad4, Feb 14, 2006 IP
  5. ian_ok

    ian_ok Peon

    Messages:
    551
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I've added this code and it all works fine, I presume when SE visit they will remove the old pages from their index and then at somepoint later I can completely delete the file??????

    This is the URL can anyone tell me thsi is fine and will cause no SE problems:
    h tt p://w ww.costaandsierra.com/property_details/viewimage.php?imageID=319&type=listing
    
    <HTML>
    <HEAD>
    <META HTTP-EQUIV="REFRESH" 
    CONTENT="10; URL=http://www.site.com">
    <TITLE>This page has moved</TITLE>
    <META http-equiv="Content-Language" content="en-us">
    </head>
    <BODY>
    <br><br>
    <P>This page has moved all images are now located on each individual property for sale page.<br><br>
    </font><a href="http://www.site.com">Click here or wait 10 seconds to be redirected automatically to our home page</A>
    </p>
    </body>
    </html>
    Code (markup):
     
    ian_ok, Feb 14, 2006 IP
  6. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I am not sure this will work as it does not send the right headers to the search engine to say the page has moved. It will work to send your visitors on but I don't think it works as well for search engines.

    A quick google of this seems to agree with my point.
     
    mad4, Feb 14, 2006 IP
  7. stuw

    stuw Peon

    Messages:
    702
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I think it is better if you you just redirect with the php. You also may want to make a sitemap of the urls that no longer exist. If you can get that page spidered and the se's to follow all the redirects with the 301 Moved Permanently header then you should see the old address removed and the new pages spidered much more quickly.
     
    stuw, Feb 14, 2006 IP
  8. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #8
    To avoid the headers already sent errors you could try putting ob_start(); at the top of your main php script and ob_flush(); at the end.
     
    mad4, Feb 14, 2006 IP
  9. ian_ok

    ian_ok Peon

    Messages:
    551
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I've done it....I added your code to the viewimage.php along with all the headers like this:

    Do I need to change the actual headers that are shown below or anything else? I have the rest of the code below this.

    Once I see most SE have removed the index of the obsolute pages I'm safe to delete the file all together?

    <?php
    header("HTTP/1.1 301 Moved Permanently"); 
    header("Location: ht tp://www.costaandsierra.com"); 
    exit(); ?>
    <HTML>
    <HEAD>
    <title>Title here</title>
    <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
    <meta name="author" content="Ian Ainsworth">
    <meta name="keywords" content="K W here">
    <meta name="description" content="Desc here
    <meta name="distribution" content="global">
    <LINK href="style here" type="text/css" rel="stylesheet">
    </HEAD>
    Code (markup):
    Thanks Ian
     
    ian_ok, Feb 15, 2006 IP
  10. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Looks fine to me. Glad its sorted.:)
     
    mad4, Feb 15, 2006 IP
    ian_ok likes this.
  11. stuw

    stuw Peon

    Messages:
    702
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #11
    you don't need anything after the
    <?php
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: ht tp://www.costaandsierra.com");
    exit();
    ?>
     
    stuw, Feb 15, 2006 IP
  12. ian_ok

    ian_ok Peon

    Messages:
    551
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Thanks Stuw, have removed everything.

    **Just 1 final question - I've got another site in Spanish language which I'll be doing the same with. I've used the following redirect in htaccess to change to SE friendly BUT to redirect to the homepage doesn't work it:

    RewriteRule ^viewimage.html viewimage.php
    RewriteRule ^viewlistingimage-([0-9]*).html viewimage.php?imageID=$1&type=listing

    So if I add the header("HTTP/1.1 301 Moved Permanently"); etc to viewimage.php will this cause problems with the htaccess saying one thing and the file doing another, all links in SE are viewlistingimage-numberhere.html

    Thanks Ian
     
    ian_ok, Feb 15, 2006 IP
  13. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #13
    The best thing is to try it and see :)

    the htacces will redirect the page before any scripts on the page have a chance to run. Therefore if the htaccess redirect is not working the php redirect will work fine.
     
    mad4, Feb 15, 2006 IP
  14. chrisd

    chrisd Well-Known Member

    Messages:
    213
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    110
    #14
    chrisd, Feb 20, 2006 IP
  15. dfsweb

    dfsweb Active Member

    Messages:
    1,587
    Likes Received:
    55
    Best Answers:
    0
    Trophy Points:
    88
    #15
    I would suggest just using .htaccess to do the redirection. It's not too tough. Just ensure that the file is names ".htaccess" (with the dot) and that it contains a line like this for every redirection you want:

    redirect 301 /oldpage.htm http://www.mysite.com/newpage.htm
    or
    redirect 301 /directory/oldpage.htm http://www.mysite.com/directory/newpage.htm

    Use the first option if the old page is in the root directory and the second if it's in a directory. Do note how the first parameter is just the page location whereas the second is the complete web address with the http and www. Hope this helps!
     
    dfsweb, Feb 21, 2006 IP
  16. jedibusiness

    jedibusiness Well-Known Member

    Messages:
    147
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    108
    #16
    Thanks for that link Chris. I have been looking all over for it lol.
     
    jedibusiness, Feb 23, 2006 IP