Connecting Tech Pros Worldwide Forums | Help | Site Map

Mod_Rewrite / Regex to PHP URL

Dr. No
Guest
 
Posts: n/a
#1: Feb 14 '07
I am new to using mod_rewrite and am trying to rewrite a URL to pass values to
my script so that:

http://www.myclient.com/everything/animals/mammals/cats

is transformed into

http://www.myclient.com/index.php?nav[0]=everything&nav[1]=animals&nav[2]=mammals@nav[3]=cats

Specifically, I am looking for an n-ary solution to this problem. I know how to
do it for a discrete number, but how do I do this transformation when the number
of variables to go into the nav array is unknown, and how do I generate the
index number? Can any experience mod_rewrite gurus point me in the right
direction. I'm sure this is a common design pattern.

Thanks.

Rik
Guest
 
Posts: n/a
#2: Feb 15 '07

re: Mod_Rewrite / Regex to PHP URL


On Thu, 15 Feb 2007 00:15:34 +0100, Dr. No <no@no.nowrote:
Quote:
I am new to using mod_rewrite and am trying to rewrite a URL to pass
values to my script so that:
>
http://www.myclient.com/everything/animals/mammals/cats
>
is transformed into
>
http://www.myclient.com/index.php?nav[0]=everything&nav[1]=animals&nav[2]=mammals@nav[3]=cats
>
Specifically, I am looking for an n-ary solution to this problem. I know
how to do it for a discrete number, but how do I do this transformation
when the number of variables to go into the nav array is unknown, and
how do I generate the index number? Can any experience mod_rewrite gurus
point me in the right direction. I'm sure this is a common design
pattern.
As far as I know you cannot, not without really messy toying with
environmental variables.

As you're using PHP, what's wrong with:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?nav=$1

And in PHP:

$nav = isset($_GET['nav']) ? explode('/',$_GET['nav']) : array();
--
Rik Wasmus
Kimmo Laine
Guest
 
Posts: n/a
#3: Feb 15 '07

re: Mod_Rewrite / Regex to PHP URL


"Dr. No" <no@no.nowrote in message
news:qKMAh.14804$O02.2869@newssvr11.news.prodigy.n et...
Quote:
>I am new to using mod_rewrite and am trying to rewrite a URL to pass values
>to my script so that:
>
http://www.myclient.com/everything/animals/mammals/cats
>
is transformed into
>
http://www.myclient.com/index.php?nav[0]=everything&nav[1]=animals&nav[2]=mammals@nav[3]=cats
>
Specifically, I am looking for an n-ary solution to this problem. I know
how to do it for a discrete number, but how do I do this transformation
when the number of variables to go into the nav array is unknown, and how
do I generate the index number? Can any experience mod_rewrite gurus point
me in the right direction. I'm sure this is a common design pattern.

Just use empty brackets: nav[]=everything&nav[]=animals&nav[]=mammals, php
will understand this and create the index numbers for you.

--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
spam@outolempi.net | rot13(xvzzb@bhgbyrzcv.arg)


Toby A Inkster
Guest
 
Posts: n/a
#4: Feb 15 '07

re: Mod_Rewrite / Regex to PHP URL


Rik wrote:
Quote:
As you're using PHP, what's wrong with:
>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?nav=$1
>
And in PHP:
>
$nav = isset($_GET['nav']) ? explode('/',$_GET['nav']) : array();
Or even:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1

And in PHP:
$_GET['nav'] = explode('/',$_SERVER['PATH_INFO']);

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Closed Thread