redirect 301

Discussion in 'PHP' started by roice, Nov 11, 2010.

  1. #1
    Hello,
    I built my website again, and because of that I have new URLs for all the texts pages.
    If I understand right, I need to do "redirect 301" to all the old url.
    for example:
    old url:
    http://www.my-site.com/index.php?page=questions
    new url:
    http://www.my-site.com/index.php?page=5

    This is example for ionly 1 url. I'm have lots of them.

    I heard that I have to add code to file ".htaccess" - what is the code to "redirect 301" for the url in my example?

    BTW - Do I realy need to do "redirect 301"? wouldn't Google will understand that the old URLs are no longer exist and that they moved to new URLs?

    Thanks
     
    Last edited: Nov 11, 2010
    roice, Nov 11, 2010 IP
  2. fr33lanc3

    fr33lanc3 Peon

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    This is the format to use in your .htaccess file.

    Redirect 301 /index.php?page=questions http://www.my-site.com/index.php?page=5

    If .htaccess does not exist in your root directory, just create a new file in a text editor, saving it as .htacess (no extension)

    Yes, you really need to do a 301 redirect. This tells google that the page has permanently moved to a new location. You don't want to risk google dropping the page from its index if it gets a 404 error response.
     
    fr33lanc3, Nov 11, 2010 IP
  3. DN48

    DN48 Peon

    Messages:
    59
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    DN48, Nov 11, 2010 IP
  4. dihuta

    dihuta Active Member

    Messages:
    47
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #4
    DN48 is right.
    It should be my-site.com/index/questions or my-site.com/index/questions-5/
    This is more SEO friendly.

    You can use .htaccess to redirect, it is better than using php.
     
    dihuta, Nov 11, 2010 IP
  5. xpertdev

    xpertdev Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Yes I agree with the dihuta.

    if you choose your new url as below:
    www.site.com/index/question

    then here is the rule which redirect 301 for all your pages.

    RewriteRule ^index/([a-zA-Z0-9-_])$ index.php?page=$1 [NC,L,R=301]

    This will surely work for you.
     
    xpertdev, Nov 11, 2010 IP
  6. sikatangpinoy

    sikatangpinoy Peon

    Messages:
    300
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    if you are using wordpress there are lots of plugins you can use to do this job.
     
    sikatangpinoy, Nov 11, 2010 IP
  7. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    roice, Nov 12, 2010 IP
  8. MartinPrestovic

    MartinPrestovic Peon

    Messages:
    213
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #8
    roice, the code you need to use is what fr33lanc3 provided in the first response.

    What the others are talking about is for implementing search engine friendly URL's.
     
    MartinPrestovic, Nov 12, 2010 IP
  9. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    roice, Nov 25, 2010 IP
  10. fr33lanc3

    fr33lanc3 Peon

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Looking at your question again, I see you are not wanting to redirect to a new file, but only changing a value in the query string. A 301 redirect wont work because you are redirecting to the same page.

    I would suggest writing a function at the top of index.php that would check for the new updates in your page value. You would need to map old values to new values. If you have 10-20, I would just do in code, if you have hundreds, I would probably create a database table to store the new values for easier management.

    logic would be something like this ==>

    //array to hold new values to map
    $mapArr['questions'] = 5;
    $mapArr['answers'] = 6; ...

    // value from query string to test
    $oldPage = $_GET['page'];

    // test if old page is in array an has new value
    if (array_key_exists($oldPage, $mapArr)) {
    $page =$mapArr[$oldPage];
    }
    else {
    // use value from query string because it has not been remapped
    $page = _GET['page'];
    }

    // continue with logic using page variable
    ...
     
    fr33lanc3, Nov 25, 2010 IP
  11. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Hi fr33lanc3
    Thank for your reply

    All the inbound links on the net, so my site, direct to the main page of the site - index.php
    the reason I want to do redirct is because I don't want that Google will think I duplicate text...
     
    roice, Nov 25, 2010 IP