RewriteRules work, but can't get PHP code right

Discussion in 'PHP' started by SEO-Expert, Oct 20, 2006.

  1. #1
    Working on a PHP script that originally used dynamic URLs, trying to make the URLs search engine friendly, but run into a problem when I try to replace spaces and other unwanted characters with hyphens (or remove them).
    Using urlencode works, it replaces spaces with a + but it results in some character codes like % 26 for & that don't work in a browser with this script. So pretty sure my RewriteRules are correct, for example this set-

    RewriteRule ^([^/]+)/Artist/([^/]+)/$ search.php?typ=$1&search=$2 [L,NC] 
    RewriteRule ^([^/]+)/Artist/([^/]+)/([^/]+)/$ search.php?typ=$1&search=$2&page=$3 [L,NC] 
    Code (markup):

    Results in URLs-

    domain.com/word1+word2/Artist/artist+name/
    domain.com/word1+word2/Artist/artist+name/1/

    When using urlencode and-

    domain.com/word1%20word2/Artist/artist%20name/
    domain.com/word1%20word2/Artist/artist%20name/1/

    When using nothing to convert spaces.

    If I add a function like-

    function safeurl($name){ 
    $retVal = str_replace('/',' ',$name); 
    $retVal = str_replace(' ','-',$retVal); 
    return $retVal; 
    } 
    Code (markup):
    With corresponding code-

    href="'.$site_url.''.safeurl($typ).'/Artist/'.safeurl($artist).'/1/ 
    Code (markup):
    It replaces spaces with a hyphen, but clicking the link fails!

    The site uses a CSV file as data source (no database).

    I'm stuck on what to try next?

    David

    PS I've asked this question on NGs and another popular forum with no joy! See test site here http://www.temphelpwnc.com/ using the urlencode code.
     
    SEO-Expert, Oct 20, 2006 IP
  2. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #2
    
    function safeurl($name){ 
    $retVal = str_replace('%20',' ',$name); 
    $retVal = str_replace('+',' ',$retVal ); 
    return $retVal; 
    }
    
    PHP:
    Peace,
     
    Barti1987, Oct 20, 2006 IP
  3. SEO-Expert

    SEO-Expert Well-Known Member

    Messages:
    328
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    140
    #3
    Hi azizny,

    I tried code like you suggest and many other alternatives and nothing worked! It's weird as I've made PHP scripts search engine friendly before with far more complicated needs.

    Only unique thing about this script compared to others I've converted is it uses a CSV file rather than a MySQL database as data source. Though I've done this with XML feeds so doubt it's the source of data.

    BTW I assumed you meant-

    function safeurl($name){
    $retVal = str_replace('%20','-',$name);
    $retVal = str_replace('+','-',$retVal );
    return $retVal;
    } 
    Code (markup):
    To convert spaces to hyphens (missed the hyphens).

    David
     
    SEO-Expert, Oct 20, 2006 IP
  4. speedwagon

    speedwagon Peon

    Messages:
    52
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    what do you use to parse $_GET['typ'] and $_GET['search']?
    because if you were searching "word1-word2" or "artist-name" it obviously isn't gonna show up for query results.
    if your safeurl() function only replaces spaces with hyphens, you might wanna add a function that parses hyphens into spaces to use in querying your db.

    edit: actually come to think about it, the artist name might have hyphens in it as well, so in that case you might wanna check the $_GET['search'] var against safeurl()ed results from your queries (probably not efficient). or you could modify all artist name entries in your db to be safeurl()ed.
     
    speedwagon, Oct 21, 2006 IP
  5. SEO-Expert

    SEO-Expert Well-Known Member

    Messages:
    328
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    140
    #5
    The data is from a CVS file so (I think) doesn't use $_GET directly uses fgetcsv

    will that make a difference?

    Example code from the script-

    
    <?php
    $file = fopen("$filepath", "r");
    echo'';
    
    while (($data = fgetcsv($file, 2048, ";")) !== FALSE) {
    if ($row>0 and $data[$DATA["typ"]]==$typ){$categories[$row]=$data[$DATA["cat"]];}
    $row++;
    }
    
    Code (markup):
    This is for creating a secondary menu (so not the Artist/Search URLs, in hindsight probably easier to work with these first) with URL structure-

    
    echo'<a href="'.$site_url.''.$typ.'/'.safeurl($categories).'/1/">'.$trimcat.'</a> |';
    
    Code (markup):
    On the main default.php page there's this code that uses get-

    
    $typ=$_GET['typ'];
    if ($typ==''){$typ=$real_music_tones[0];}
    
    $cat=$_GET['cat'];
    
    $sct=$_GET['sct'];
    $page=$_GET['page'];
    
    Code (markup):
    Tried using the replace function directly on these and no joy :-(

    And on the search.php page there's this code -

    
    $search=$_POST['search'];
    if ($search=="") {$search=$_GET['search'];}
    $search=trim($search);
    
    $lcase_search=strtolower($search);
    
    $page=$_GET['page'];
    if ($page==''){$page=1;}
    
    $typ=$_POST['typ'];
    if ($typ=="") {$typ=$_GET['typ'];}
    
    Code (markup):
    Does that help at all?

    David
     
    SEO-Expert, Oct 23, 2006 IP
  6. SEO-Expert

    SEO-Expert Well-Known Member

    Messages:
    328
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    140
    #6
    Managed to solve the problem.

    Thanks for the point in the right direction speedwagon, didn't click right away but know what you meant now.

    BTW there are some database entries with hyphens (typical hey). At first they didn't work because the two sets of replace functions clashed, but managed to come up with compromises that worked like changing space-space to ~to~ so entries like A - C comes out as A~to~C as a URL which meant I could then change ~to~ back to space-space for the script to work.

    Is there a better way of doing this so I can replace say spaces, slashes and a bunch of other characters all to hyphens?

    David
     
    SEO-Expert, Oct 24, 2006 IP
  7. speedwagon

    speedwagon Peon

    Messages:
    52
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    have your safeurl() function to str_replace whatever characters you want to convert into hyphens, then in your default.php, make sure you do $variable = safeurl($_GEt['variable']), then change this part from your script code above:
    
    while (($data = fgetcsv($file, 2048, ";")) !== FALSE) {
    if ($row>0 and $data[$DATA["typ"]]==$typ){$categories[$row]=$data[$DATA["cat"]];}
    $row++;
    }
    
    Code (markup):
    to
    
    while (($data = fgetcsv($file, 2048, ";")) !== FALSE) {
    if ($row>0 and safeurl($data[$DATA["typ"]])==$typ){$categories[$row]=$data[$DATA["cat"]];}
    $row++;
    }
    
    Code (markup):
    pretty much apply same deal (maybe not exaactly the same but the gist of it) with whatever you're querying your db with in accordance with your rewrite rules

    btw, you can take out the !==FALSE part in your while statement.

    hope that helps
     
    speedwagon, Oct 25, 2006 IP