Mod-rewrite help

Hi everyone,

I’m trying URI/URL rewriting and as expected can’t get it to work. I’ve based my example on the Sitepoint article - Learn Apache mod_rewrite: 13 Real-world Examples http://www.sitepoint.com/apache-mod_rewrite-examples/

I’m using localhost to test my website.

Presently my URL’s have this format (in the web browser):

http://localhost/somefolder/somepage.php?city=San Diego&country=USA

But using mod_rewrite I’d have:

http://localhost/somefolder/San Diego/USA

Must I now have an actual link in my HTML, such as the one below, which is then redirected to the one with the query string?

<a href="/somefolder/San Diego/USA">San Diego</a>

My rewrite rule consists of:

RewriteEngine on

RewriteRule ^/?([a-zA-Z_]+)/([a-zA-Z_]+)$ somepage.php?city=$1&country=$2 [L]

Thank you for any assistance!

> Must I now have an actual link in my HTML, such as the one below, which is then redirected to the one with the query string?

That’s the whole point of rewriting, right? To have a particular style of url used externally to the server. And not have to organise your files on the server to literally reflect what the external urls suggest is the organisation in the server.

That’s the whole point of rewriting, right? To have a particular style of url used externally to the server. And not have to organise your files on the server to literally reflect what the external urls suggest is the organisation in the server.

Yes, thanks, just wanted to confirm that.

I tried the rewriting but receive a 404 Not Found:

The requested URL /somefolder/San Diego/USA was not found on this server.

I’ve already posted my code in the first post. Could someone please have a look at it and offer an explanation as to why I’m receiving the error?

Thanks in advance!

One problem, you’ve got a space in your external address: /somefolder/San Diego/USA. That’s generally (always I think) not good, don’t do it. So I’d suggest fixing that aspect first, before looking at possible problems with mod rewrite.

Hi johnyboy,

thank you for your help.

I got rid of the spaces in the URL and replaced these with dashes. The rewriting does work but I’m not sure how to store the city and country names in the database. At the moment they are stored with spaces, such as San Diego. In order for a query to work, I had to include a dash between the words in the database as well, like so: San-Diego. This is OK, but if I now echo out the city name I have the dashes between the words.

Must I now use a PHP function to remove the dashes when I echo the city names, or am I doing something wrong?

Thanks again!!!

> Must I now use a PHP function to remove the dashes when I echo the city names, or am I doing something wrong?

That’s one way of doing it. You’d use str_replace to replace hyphens for spaces. Alternatively you could use str_replace to replace hyphens for spaces before you put it into your database. Consider if there are any names which should have a hyphen in, then you’d want to make those places exempt from the hyphens for spaces replacement. A third alternative is to have two columns in your db table, one for url component, and one for display name.

Ok, I’ll try that. Thanks a lot for your help, appreciate it.

Hi everyone,

I’m still busy learning about URL rewriting - I think this is going to take some time.

What I’m now trying to do is apply the URL rewriting to my pagination links. What should I do with a link such as the following?

for ($i = 1; $i <= $pages; $i++) {
		if ($i != $current_page) {
		echo '<a href="index.php?s='.(($d * ($i - 1))).'&p='.$pages.'&category='.$cat.'&city='.$city.'">'. $i.'</a>';
		} else {
			echo $i . ' ';
		}
	}

In my actual HTML I will have to replace this code with the “friendly” URL that my site’s users will see. But then what to do with the above code? Put it all into the htaccess file?

Please help me out. Thank you very much.

Hi all,

let’s say I have an URL such as the following


http://www.example.com/USA/San_Diego

How do I ensure that the first atom will only match country names and the second atom only city names?

A query won’t work if it ends with a condition such as “WHERE country=USA&city=bozotheclown”. This problem will only occur if someone manually changes the URL but still…

I believe one can limit what the expression should match (see below) but what if there are hundreds of city or country names in the database? Can’t one compare the atom to an array of acceptable values?

^/?(USA|Canada|Mexico)/([a-zA-Z_]+)$

Thank you for your help!!