Using mod_rewrite to retrieve value in mySQL using php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • narian
    New Member
    • Apr 2007
    • 1

    Using mod_rewrite to retrieve value in mySQL using php

    Hi,

    I am trying to convert urls from this format:

    http://localhost/index.php?secti on=11
    (where 11 is the id of the row in a table)

    to this friendly url:

    http://localhost/services.html (where 'services' is the sef id in the same row of the table)

    I've noticed that I need to use rewritemap and a php script to access the mysql table since each row id can be retrieved by using the sef id.

    The mapping of the text file works fine for me. I just want to use a PHP script instead since my supervisor asked me too (as an exercise, even though I know it's too heavy on the server to request the script each time - so I won't be using it in the end).

    This is what I have so far in my conf file:

    # load the map file
    # RewriteMap pages txt:/etc/apache2/map.txt (i know this works)
    RewriteMap pages prg:/etc/apache2/sef.php (this is my php script)

    # if the request isn't for a real file
    RewriteCond %{SCRIPT_FILENA ME} !-f
    # and a real directory
    RewriteCond %{SCRIPT_FILENA ME} !-d
    # grab the directory name and get the id from the map file.
    # it defaults to 0 if it's no found
    RewriteRule ^/(.*).html/?$ /index.php?secti on=${pages:$1|0 } [L]

    In my sef.php file:

    <?php

    @$db = new mysqli('localho st','username', 'password','db' );

    $query = "Select id from expv5_section where sefurl = 'services'";
    //On the line above, services will be an input the user types in the URL but I haven't
    //got to that stage yet.

    $result = $db->query($query );

    $row = $result->fetch_assoc( );
    return $row['id'];

    ?>

    But this doesn't seem to work. I know the php file returns the right id too. I think it doesn't like the following line, because my apache server doesn't even restart when I add it:

    RewriteMap pages prg:/etc/apache2/sef.php

    but I don't know what else the problem could be. Should I take a different approach with mod_rewrite?

    I've googled for the past few hours and the more I read the more I'm confused.
    Each time I stumble across this site for problems I always find it's useful, so I do hope someone can help me out. :0)

    Many thanks.
Working...