views:

407

answers:

2

The following url works ok:

http://localhost/index/index/

However, I am unable to _get$ variables when they come in like this:

http://localhost/index/index/test/1234/test2/4321

-but-

I can however, _get$ the variables these ways:

http://localhost/index.php?test=1234&test2=4321
http://localhost/index?test=1234&test2=4321
http://localhost/index/index?test=1234&test2=4321

Why does are the variables not showing up for me when I use the /index/index/var/val way?

Below you will find my .htaccess file.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
A: 

Because $_GET contains the variables in the query string - that's the part of the URL after the question mark. Notice that the rewriting rules in your .htaccess file turn all URLS that do not refer to existing files or directories into just index.php, without any trace of the original URL (though, as Gumbo's comment reminded me, it's still accessible via $_SERVER['REQUEST_URI']. Your RewriteRules don't create a query string (i.e. they don't put a question mark into the URL), which is what you'd need to do to use $_GET.

I would suggest replacing your last RewriteRule with something like

RewriteRule ^.*$ index.php$0 [NC,L]

That $0 will append the original URL to index.php - so for example, http://localhost/index/index/test/1234/test2/4321 will become http://localhost/index.php/index/index/test/1234/test2/4321 Then the request will be handled by index.php and the $_SERVER['PATH_INFO'] variable will be set to the original URL, /index/index/test/1234/test2/4321. You can write some PHP code to parse that and pick out whatever parameters you want.

If you don't want the /index/index at the beginning to be saved in the path_info variable, you can use a RewriteRule like this instead:

RewriteRule ^/index/index(.*)$ index.php$1 [NC,L]

or

RewriteRule ^(/index)*(.*)$ index.php$2 [NC,L]

to strip off any number of leading /indexes.

EDIT: actually, you can keep your existing RewriteRules and just look at $_SERVER['REQUEST_URI'] to get the original request URI; no messing around with the path info is needed. Then you can split that up as you like in PHP.

David Zaslavsky
Mike Curry
The original requested URL path and query is not lost (see $_SERVER['REQUEST_URI']).
Gumbo
@Gumbo: good point, I completely forgot about that
David Zaslavsky
seems my problem is with my lack of knowledge with Zend, rather than my lack of knowledge with mod_rewrite, lol. Ty for the help though.
Mike Curry
hm, well... I wrote my answer assuming you were using plain PHP, no frameworks. Oops I guess :-/
David Zaslavsky
+3  A: 

Zend Framework doesn't make data in the request uri available as $_GET variables, to access them, use the key in a controller:

$test = $this->getRequest()->getParam('test') //$test = 1234

Or shorter

$test = $this->_getParam('test');
David Caunt
This is the answer... I guess I got offtrack, as I was using _GET$ with the Zend_Filter_Input.
Mike Curry