Zend / Apache mod_rewrite ......怎么了?

时间:2022-11-24 11:22:29

The following url works ok:

以下网址正常:

http://localhost/index/index/

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

但是,当他们像这样进来时,我无法_get $ variables:

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

-but-

I can however, _get$ the variables these ways:

但是,我可以通过以下方式_get $变量:

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?

当我使用/ index / index / var / val方式时,为什么变量没有显示给我?

Below you will find my .htaccess file.

您将在下面找到我的.htaccess文件。

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

2 个解决方案

#1


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

Zend Framework不会将请求中的数据uri作为$ _GET变量提供,要访问它们,请使用控制器中的密钥:

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

Or shorter

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

#2


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.

因为$ _GET包含查询字符串中的变量 - 这是问号后面的URL的一部分。请注意,.htaccess文件中的重写规则将所有不引用现有文件或目录的URL转换为index.php,没有任何原始URL的痕迹(但是,正如Gumbo的评论提醒我的那样,它仍然可以通过$ _SERVER访问) ['REQUEST_URI']。您的RewriteRules不会创建查询字符串(即它们不会在URL中添加问号),这是您使用$ _GET所需要做的事情。

I would suggest replacing your last RewriteRule with something like

我建议用类似的东西替换你的最后一个RewriteRule

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.

$ 0会将原始URL附加到index.php - 例如,http:// localhost / index / index / test / 1234 / test2 / 4321将变为http://localhost/index.php/index/index/test / 1234 / test2 / 4321然后请求将由index.php处理,$ _SERVER ['PATH_INFO']变量将设置为原始URL,/ index / index / test / 1234 / test2 / 4321。您可以编写一些PHP代码来解析它并选择您想要的任何参数。

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:

如果您不希望将开头的/ index / index保存在path_info变量中,则可以使用这样的RewriteRule:

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.

编辑:实际上,您可以保留现有的RewriteRules,只需查看$ _SERVER ['REQUEST_URI']即可获取原始请求URI;不需要弄乱路径信息。然后你可以在PHP中将其拆分。

#1


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

Zend Framework不会将请求中的数据uri作为$ _GET变量提供,要访问它们,请使用控制器中的密钥:

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

Or shorter

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

#2


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.

因为$ _GET包含查询字符串中的变量 - 这是问号后面的URL的一部分。请注意,.htaccess文件中的重写规则将所有不引用现有文件或目录的URL转换为index.php,没有任何原始URL的痕迹(但是,正如Gumbo的评论提醒我的那样,它仍然可以通过$ _SERVER访问) ['REQUEST_URI']。您的RewriteRules不会创建查询字符串(即它们不会在URL中添加问号),这是您使用$ _GET所需要做的事情。

I would suggest replacing your last RewriteRule with something like

我建议用类似的东西替换你的最后一个RewriteRule

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.

$ 0会将原始URL附加到index.php - 例如,http:// localhost / index / index / test / 1234 / test2 / 4321将变为http://localhost/index.php/index/index/test / 1234 / test2 / 4321然后请求将由index.php处理,$ _SERVER ['PATH_INFO']变量将设置为原始URL,/ index / index / test / 1234 / test2 / 4321。您可以编写一些PHP代码来解析它并选择您想要的任何参数。

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:

如果您不希望将开头的/ index / index保存在path_info变量中,则可以使用这样的RewriteRule:

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.

编辑:实际上,您可以保留现有的RewriteRules,只需查看$ _SERVER ['REQUEST_URI']即可获取原始请求URI;不需要弄乱路径信息。然后你可以在PHP中将其拆分。