Changing Urls via PHP?

Hi Everyone,

I’ve partly figured out how to change my URLs but I’m having trouble with fixing the space(%20) that’s located in my URL.

I’ve tried using the str_replace function but it’s either not working or I’m not doing in correctly.

My goal is to turn this URL (first one below this statement) into the URL that’s below the first one. I’m having a hard time figuring this out. Any help would be appreciated. Thanks Everyone!

(URL that works but it has %20, which is something I don’t want)

(URL that does not work but I’m attempting to make the above URL look like this one)

My .htaccess code is below as well. Thanks again everyone!

include('init.php'); // connection to database

// if city exists...
if (isset($_GET['u'])) {
    // $city = str_replace(' ','-');
    // decode and replace hyphen with space
    // $city = str_replace('-','',urldecode($_GET['u']));
	// $city = str_replace('%20','-',urldecode($_GET['u']));
	   $city = str_replace('%20','-',urldecode($_GET['u']));
	// $city = str_replace(urldecode($_GET['u']),' ','-');
	// $city = str_replace('','-',urldecode($_GET['u']));
	// $city = str_replace('-','','%20','-', 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"].'<br>';
				echo $row["State"].'<br>';
				echo 'This is what I get when I echo $_GET '.$_GET['u'].'<br>';
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(.*) /auto/cars.php?s=$1&u=$2 [L,NC]

If I understand correctly, you simply need to apply the PHP urldecode() method to the string(s).

You’re URLDecoding the variable, which changes “Key%20West” into “Key West” and then doing a str-replace on it looking for %20… but the %20 is already gone, so you end up with “Key West” again.


I see StarLion also came to the same conclusion

$city = str_replace('%20','-',urldecode($_GET['u'])); 

Because you are using urldecode(), %20 is no longer valid, %20 is now represented as a space.

So the following should work:

$city = str_replace(' ','-',urldecode($_GET['u'])); 

Hey Everyone,

I appreciate all of the responses. Cpradio, thanks for the syntax but it’s not working for some reason.

I tried

$city = str_replace(' ','-',urldecode($_GET['u']));  

but my page isn’t echoing anything.

Have you performed a var_dump($_GET[‘u’]) and on $city to see what data you are receiving?

Hey cpradio,

Thanks for the response. In order to show you the var_dump I had to slightly change the syntax

(it’s currently this way to show you the var dump)

$city = str_replace('','-',urldecode($_GET['u'])); 


$city = str_replace(' ','-',urldecode($_GET['u'])); 

To answer your question, the var_dump is echoing out

string(8) “key west”

Okay, so what value are you trying to get for your query?

I’m trying to take the “%20” and turn that into a -. So my URL will look like this

Thanks again cpradio for the help!

but nothing in your PHP is building a URL, it is reading from the URL…

I thought that’s what I had to do in order to make the URLS that I wanted. If you were me, what would you do?

Your rewrite will handle the format of your URLs

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

That should already be passing key-west to your PHP script, which means, you then need to replace “-” with " " so it can find it in your table.

Thanks CPradio! It’s working! Yippy!

But let me ask you this, is their easier way to do this?


The removing of the hyphen, or achieving the URL format you now want?

In either case, no, not really. You are doing it right. The only thing I would mention is that if you ever want to replace multiple characters, to use preg_replace() instead of creating a str_replace for each one (especially if you are going to replace each character with a space).

But you are using RewriteRules to do what they were intended, granted, @dklynn ; would give you a very long drawn out rant over the use of (.*), as it is a catch all, when you likely could have used ([a-zA-Z0-9 -]+)

Thanks a lot cpradio. I appreciate your help!