Kladskull
Kladskull

Reputation: 10762

Zend/Apache mod_rewrite... what's wrong?

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]

Upvotes: 0

Views: 998

Answers (2)

David Z
David Z

Reputation: 131800

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.

Upvotes: 0

David Snabel-Caunt
David Snabel-Caunt

Reputation: 58379

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');

Upvotes: 3

Related Questions