PHP URL Rewriting / .htaccess question

Discussion in 'PHP' started by scm22ri, Sep 4, 2012.

  1. #1
    Hi Everyone,


    I'm trying to append another value onto my URL that I'm dynamically inserting via php syntax in the .htaccess, I'm having trouble trying to figure it out.


    I want to add the state that's associated with the city in the URL. How would I do that?


    I want the URLS to look like this. (the below URLS don't work)
    http://whatsmyowncarworth.com/auto/albany/new-york
    http://whatsmyowncarworth.com/auto/Miami/florida
    http://whatsmyowncarworth.com/auto/providence/rhode-island


    In the URLS below I'm getting the "city" and appending it to the URL. (my php syntax is below). Now I want to add the state after the city. How would I do that?
    http://whatsmyowncarworth.com/auto/Miami
    http://whatsmyowncarworth.com/auto/providence
    http://whatsmyowncarworth.com/auto/albany


    This is my syntax and below that is my .htaccess code. Any help would be great. Thanks everyone!

    <?phpinclude('init.php'); // connection to database
    // if city exists...if (isset($_GET['u'])) {    // decode and replace hyphen with space    $city = str_replace('-','',urldecode($_GET['u']));        // if value contains only letters, numbers or spaces...    if ( preg_match('~^[a-z0-9 ]+$~i',$city) ) {        // select data from database...        $data = mysql_query("SELECT State, City FROM cars WHERE City='$city'" );        if (mysql_num_rows($data) > 0) {            while ($row = mysql_fetch_assoc($data)) {                                echo $row["City"];                 echo $row["State"];            }        }    }}?>
    PHP:

    >>>>>>>>>>>>>>>>

    .htaccess

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /auto/cars.php?u=$1 [NC]
    
    Code (markup):
     
    scm22ri, Sep 4, 2012 IP
  2. Syndication

    Syndication Active Member

    Messages:
    351
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    90
    #2
    RewriteEngine on
    RewriteRule ^auto/([^/\.]+)/?$ cars.php?u=$1 [L]
    RewriteRule ^auto/(.*)/(.*)$ cars.php?s=$1&u=$2 [L]
    
    Code (markup):
    This is how I would do it.

    Edit: Made a correction to mod_rewrite code.
     
    Syndication, Sep 6, 2012 IP