Best way to do a large number of redirects...

Discussion in 'PHP' started by PHP_Tyro, Sep 22, 2007.

  1. #1
    I'm coming from an ASP environment and need to convert a website for hosting on Linux/Apache. I think I can handle most of it bit by bit, but there's one thing I need advice on.

    I have a 404.asp file that handles a lot of redirections. My explorations so far tell me that the way to handle redirection with Linux/Apache is to include a line like the following in the .htaccess file:

    redirect 302 /DICTIONARY http://www.dictionary.com

    Simple enough, but what bothers me about that method is the number of redirects that have to be inserted in the file -- at the moment it would add over 400 lines to .htaccess, and more will need to be added over time.

    Is the size of .htaccess an important consideration?

    Is there a better way to do this? I like the idea of having a single plain text file containing all the redirections and calling it into a server-side page for processing.

    Also, I don't understand the difference between 302 and 301 redirects. Temporary and permanent, yes, but what is the effective difference? I've seen a few articles that talk about 301/302 and page rank, but I don't care about maintaining page rank in this case, because I'm referring/redirecting to sites not owned by me and over which I have no control. For example they might be sites for which I am an affiliate, or sites I am referring my visitor to for more information, or for a download or whatever.

    So in that case does it matter whether I use 301 or 302?

    Many thanks,
    Billy
     
    PHP_Tyro, Sep 22, 2007 IP
  2. bubbles19518

    bubbles19518 Peon

    Messages:
    73
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I don't quite understand what your asking but the redirection method I use in PHP is the header() fuction. For example:

    header("Location:http://google.com");

    Would redirect to google.
     
    bubbles19518, Sep 22, 2007 IP
  3. PHP_Tyro

    PHP_Tyro Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Well, to put it another way...

    The situation is that on my site, or in an e-book, or e-mail, or any similar source, a link like this would be given:

    http://www.mydomain.com/dictionary

    Clicking that link should take them to:

    http://www.dictionary.com

    There are over 400 similar redirections to be taken care of; this sort of thing:

    http://www.mydomain.com/abc -> http://www.abc.com
    http://www.mydomain.com/def -> http://www.def.com
    http://www.mydomain.com/ghi -> http://www.def.com/ghi
    http://www.mydomain.com/jkl -> http://www.jkl.com
    http://www.mydomain.com/mno -> http://www.xyz.com/123/mno

    etc.
     
    PHP_Tyro, Sep 22, 2007 IP
  4. liam1412

    liam1412 Active Member

    Messages:
    387
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #4
    But why do you need to link it to your site at all. Can't you just link to the site direct in the href??

    Sorry if I misunderstand.

    They only other way I can think is have a table called redirects and just link to redirect php for example

    <a href="mypage.com/redir.php?linkid=1">www.dictionary.com</a>

    Then in redir.php query the db to find out where you need to divert to be to.

    $linkid = $_GET['linkid'];
    
    $getlinkto = mysql_query("SELECT * FROM table WHERE linkid='$linkid'");
    $linkarray = mysql_fetch_array($getlinkto);
    
    $link = $linkarray['linkto'];
    
    header("location:$link");
    PHP:
    That make sense
     
    liam1412, Sep 23, 2007 IP
  5. PHP_Tyro

    PHP_Tyro Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    sorry for the delay, I've had to go away and work on something else for a while, but now I'm back to this. So to answer your question...

    In an article, white paper or e-book (for example) once the publication has been distributed the author no longer has any control over it.

    If, at some later date, one of the URLs referenced in the publication happens to change or disappear, the publication then has less value.

    It has long been my practice to keep a "lookup" redirection file on one of my own websites and use URLs in my publications that refer to the lookup file rather than to the target URL directly.

    If the target URL should change at a later date I only need to modify the redirection in the lookup file to compensate.

    All my work in this regard has been coded in ASP, but more and more I am using plug-ins, code snippets and scripts that are written in PHP, so it only makes sense to convert my redirection code to PHP also.

    This brings me back to my original question, which I will now restate so that everything is here in the one post...

    My redirections have been handled in a file called 404.asp (for obvious reasons), and in one of my publications a link like this might be given:

    http://www.mydomain.com/dictionary​

    Clicking that link should take the reader to: http://www.dictionary.com

    In my redirection file there are currently over 400 similar redirections - this sort of thing:

    http://www.mydomain.com/abc -> http://www.abc.com
    http://www.mydomain.com/def -> http://www.def.com
    http://www.mydomain.com/ghi -> http://www.def.com/ghi
    http://www.mydomain.com/jkl -> http://www.jkl.com
    http://www.mydomain.com/mno -> http://www.xyz.com/123/mno​

    I understand that the usual way to handle redirection with Linux/Apache is to include a line like the following in the .htaccess file:

    redirect 302 /DICTIONARY http://www.dictionary.com​

    The problem with my using that method is the number of redirects that have to be inserted in the file. As I said, at the moment it would add over 400 lines to .htaccess, and more will need to be added over time. So...

    • Is the size of .htaccess an important consideration?
    • Is there a better way to do this?

    I like the idea of having a single plain text file containing all the redirections and calling it into a server-side page for processing.

    Also, I don't understand the difference between 302 and 301 redirects. Temporary and permanent, yes, but what is the effective difference? I've seen a few articles that talk about 301/302 and page rank, but I don't care about maintaining page rank in this case, because I'm referring/redirecting to sites not owned by me and over which I have no control. For example they might be sites for which I am an affiliate, or sites I am referring my visitor to for more information, or for a download or whatever.

    So in that case does it matter whether I use 301 or 302?

    But most importantly, what is the best way to handle my redirection requirements, given the number of redirections required?
    -
    -
     
    PHP_Tyro, Jan 3, 2008 IP
  6. Panjabi

    Panjabi Peon

    Messages:
    362
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    PHP_Tyro The method suggeted by liam1412 will work for you. If the links change all you are required to do is update the links table.
     
    Panjabi, Jan 3, 2008 IP