Forum Moderators: phranque

Message Too Old, No Replies

Mod_rewrite - some variables with '-' some without

         

marakkesh

7:41 am on Dec 7, 2004 (gmt 0)

10+ Year Member



My variables to access the database can be either one or two words, for example -

selectedCity=united kingdom

or

selectedCity=germany

In preparing my site to use mod_rewrite with friendly urls, I have added hyphens to those variables that are more than one word:

selectedCity=united-kingdom

or

selectedCity=czech-republic

Is there a rule to make sure that the hyphen isn't parsed along with my real variable (united kingdom)

My rewrite code follows:

RewriteRule ^destinations/([a-zA-Z]*)/?$ /articles.php?selectedCity=$1 [L]
RewriteRule ^destinations/([a-zA-Z]*)/([a-zA-Z]*)/?$ /articles.php?theCountry=$1&selectedCity=$2 [L]
RewriteRule ^destinations/([a-zA-Z]*)/([a-zA-Z]*)/([0-9]*)\.html$ /read.php?theCountry=$1&theCity=$2&aid=$3 [L]

Thanks,

Gordon

jdMorgan

1:50 am on Dec 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> Is there a rule to make sure that the hyphen isn't parsed along with my real variable (united kingdom)

I don't understand that question, and I suspect it is because you believe that mod_rewrite is smarter than it really is. Your script replaces spaces with hyphens in URLs on your pages, and converts them to "spider-friendly" format. Browsers and 'bots request those URLs. When a request arrives at your server, mod_rewrite converts the parameters in the URL to query string format and calls your script. Your script should then convert the hyphens back to spaces and look up the city or country in your database. This hyphen-to-space conversion can be done easily in php with preg_replace:


$spaced_city = preg_replace('/-/',' ',$hyphenated_city);

Jim

marakkesh

2:35 am on Dec 8, 2004 (gmt 0)

10+ Year Member



Hi Jim -

part of my confusion comes from the fact that for some reason my variables with hyphens aren't being passed to the correct page -

if I type in /destinations/spain

there isn't a problem.

If I type in /destinations/czech-republic

Tit doesnt work.

If, however, I go directly to the page mod_rewrite is parsing to:

/articles.php?selectedCity=czech-republic

it works... I have tried echoing the variable in the session file which is the very first thing that is processed on articles.php, and any hyphenated variables don't even echo, which I find very bizzare.

jdMorgan

2:47 am on Dec 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your pattern "[a-zA-Z]*" won't accept a hyphen, so I'd suggest the pattern "[^/]+" or "[^/]*" as shown below, which will accept anything in that parameter except a slash.

In order to see what the mod_rewrite output looks like, you can change your rule(s) (temporarily) to an external redirect. This will make the rewritten URL show in your browser address bar, which may help you diagnose the problem.


RewriteRule ^destinations/([^/]*)/([^/]*)/([0-9]*)\.html$ http://www.example.com/read.php?theCountry=$1&theCity=$2&aid=$3 [R=301,L]

Jim

marakkesh

3:16 am on Dec 8, 2004 (gmt 0)

10+ Year Member



Thanks jim - exactly what I was looking for.

Gordon