creating a logical flow

New to mod_rewrite? This is a good place to start.

creating a logical flow

Postby mikeyb2009 » Sun Oct 04, 2009 12:23 pm

Hi All,

What I want to achieve is for my URLs to be like:

mysite.com/destinations/Country_name/City_name

For example:
mysite.com/destinations/France/Paris

I would ideally like to use two difference files for country information and city information, as both have very different characteristics and information layout.

The end result is that when someone is in mysite.com/France/Paris and then decide to delete the '/Paris' part at the end, they will be taken to the city page to the country page, in this example France at mysite.com/France

What is the best way to achieve this?

I hope this makes sense.
mikeyb2009
 
Posts: 4
Joined: Sat Jun 06, 2009 11:27 pm

Postby richardk » Sun Oct 04, 2009 1:23 pm

What characters are in country and city names?

You just need two rules like
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# /destinations/Country_name --> /country_information?country=Country_name
RewriteRule ^destinations/([a-z]+)/?$          /country_information?country=$1      [QSA,L]

# /destinations/Country_name/City_name --> /country_information?country=Country_name&city=City_name
RewriteRule ^destinations/([a-z]+)/([a-z]+)/?$ /city_information?country=$1&city=$2 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby mikeyb2009 » Sun Oct 04, 2009 2:40 pm

For countries e.g /France this works:

Code: Select all
RewriteRule ^([^/]+)/?$ country.php?URL=$1 [QSA,L]


with this query:
Code: Select all
$colname_country_info = "-1";
if (isset($_GET['URL'])) {
  $colname_country_info = $_GET['URL'];
}
mysql_select_db($database_explorer, $explorer);
$query_country_info = sprintf("SELECT * FROM Countries WHERE URL = %s"


but for /France/Paris, this does not work:

Code: Select all
RewriteRule ^([^/]+)/?$ country.php?URL=$1 [QSA,L]
RewriteRule ^([^/]+)/([a-z]+)/?$ city.php?Countries.URL=$1&cities.city_name=$2 [QSA,L]


With this Query:
Code: Select all
$KTColParam1_city2 = "-1";
if (isset($_GET["Countries.URL"])) {
  $KTColParam1_city2 = $_GET["Countries.URL"];
}
$KTColParam2_city2 = "-1";
if (isset($_GET["cities.city_name"])) {
  $KTColParam2_city2 = $_GET["cities.city_name"];
}
mysql_select_db($database_explorer, $explorer);
$query_city2 = sprintf("SELECT Countries.URL, cities.city_name FROM (cities LEFT JOIN Countries ON Countries.country_id=cities.parent_country) WHERE Countries.URL=%s  AND cities.city_name=%s "


Where have I gone wrong?
mikeyb2009
 
Posts: 4
Joined: Sat Jun 06, 2009 11:27 pm

Postby richardk » Mon Oct 05, 2009 8:14 am

[a-z] only matches lowercase letters. Adding the NC flag will make it case insensitive (including destinations, or you could use [a-zA-Z]).

Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^destinations/([a-z]+)/?$          /country_information?country=$1      [NC,QSA,L]
RewriteRule ^destinations/([a-z]+)/([a-z]+)/?$ /city_information?country=$1&city=$2 [NC,QSA,L]


with this query:

For mod_rewrite debugging, the value of the $_GET variables is the only important thing.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 20 guests

cron