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

The following url works fine:

http://localhost/index/index/

      

However, I cannot _get $ variables when they come in like this:

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

but -

I can, however, _get $ variables in the following 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 are the variables not showing up for me when I use the path / index / index / var / val?

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]

      

0


a source to share


2 answers


Zend Framework does not make the data in the uri request available as $ _GET variables, to access it, use the key in the controller :

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

      



Or shorter

$test = $this->_getParam('test');

      

+3


a source


Because it $_GET

contains variables in the query string - this is the part of the URL after the question mark. Please note that the rewrite rules in your file .htaccess

turn all URLS that do not refer to existing files or directories into just index.php

no trace of the original URL (although, as Gumbo's comment reminded me, it is still available via $_SERVER['REQUEST_URI']

yours RewriteRule

does not create a query string (i.e. does not put a question mark in the url) which is what you need to do in order to use $_GET

.

I would suggest replacing your last one RewriteRule

with something like

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

      

Something that $0

will add to the original URL index.php

- for example, http://localhost/index/index/test/1234/test2/4321

will http://localhost/index.php/index/index/test/1234/test2/4321

then request to be processed index.php

, the variable $_SERVER['PATH_INFO']

will be set to the original URL, /index/index/test/1234/test2/4321

. You can write some PHP code to parse it and pick whatever options you want.

If you don't want it to be /index/index

stored in the path_info variable at the beginning, you can use instead RewriteRule

:



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

      

or

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

      

to remove any number of leading /index

es.

EDIT: Actually you can keep the existing one RewriteRule

and just look $_SERVER['REQUEST_URI']

to get the original request URI; no need to fiddle with path information. Then you can parse this however you like in PHP.

0


a source







All Articles