SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Evangelist
    Join Date
    Jan 2005
    Location
    UK
    Posts
    539
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    if space in url do 301 redirect

    Hi all,

    I have discovered that Google has indexed 2 urls for same page, one with a dash and one with a space (contains %20 in url in listings).

    I need to do a 301 redirect within the php page to detect if the space version has been used and go to the correct dashed version.

    I have tried to find the space, but its not finding it and redirecting:

    PHP Code:
    //array of possible space characters to search for
    $space_array = array(" ","%20","\s");

    // get page name
    $name clean($_GET['name']);
    // replace dash for content purposes
    $name2 str_replace("-"," ",$name);
    // replace spaces with dash for 301 
    $name3 str_replace($space_array,"-",$name);

    //if we find a space in url then do 301 to actual dashed page
    if ( stristr($space_array,$name)  ){
        
    header('HTTP/1.1 301 Moved Permanently');
        
    header("Location: http://www.mywebsite.co.uk/$name3-page.html");
        die();

    Any ideas?

  2. #2
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I bet some debugging would help.
    Do you have an idea of debugging?
    even simple var_dump()-ing used variables will show the traitor.

    add this code above condition:
    PHP Code:
    $found=stristr($space_array,$name);
    var_dump($found,$name,$name2,$name3);
    die(); 
    and see what it say

    also reduce $space_array to just " " string. other two members are useless

  3. #3
    SitePoint Evangelist
    Join Date
    Jan 2005
    Location
    UK
    Posts
    539
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    its saying false, but yet the printed variable has a space in it! I've ditched the space_array now and just using " ".

  4. #4
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh sure. what a dumb I am!
    stristr just doesn't support arrays

    you have to swap params too
    reading manual before using a function would help for sure

  5. #5
    SitePoint Evangelist
    Join Date
    Jan 2005
    Location
    UK
    Posts
    539
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    but still not working though...does stristr not find empty space written as " ".

  6. #6
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've edited my post.
    read manual entry for stristr carefully and arrange params in the right way

  7. #7
    SitePoint Evangelist
    Join Date
    Jan 2005
    Location
    UK
    Posts
    539
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    damn i tried other way around, but when using space array!! working now cheers


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •