Page 1 of 1

PHP URL Rewriting / .htaccess question

Posted: Tue Sep 04, 2012 1:02 pm
by mcc_shane
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/prov ... ode-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!

Code: Select all

<?php
include('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"];
            }
        }
    }
}
?>
>>>>>>>>>>>>>>>>>>>>>>>>>

.htaccess

Code: Select all

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /auto/cars.php?u=$1 [NC]

Re: PHP URL Rewriting / .htaccess question

Posted: Thu Sep 06, 2012 10:40 am
by Live24x7
i don't think you can do that dynamic insertion of state name with .htaccess.
would have to be done with php

Something like this, may be:

Code: Select all

echo '<a href="http://whatsmyowncarworth.com/auto/'.$row["State"]."/".$row["City"].'">'.$row["City"].'</a>';