[英]Zend/Apache mod_rewrite… what's wrong?
以下網址可以正常運行:
http://localhost/index/index/
但是,當它們像這樣出現時,我無法_get $變量:
http:// localhost / index / index / test / 1234 / test2 / 4321
-但-
但是,我可以通過以下方式_get $變量:
http://localhost/index.php?test=1234&test2=4321
http://localhost/index?test=1234&test2=4321
http://localhost/index/index?test=1234&test2=4321
為什么在使用/ index / index / var / val方式時變量沒有顯示出來?
在下面,您可以找到我的.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]
Zend Framework不會將請求uri中的數據作為$ _GET變量提供,要訪問它們,請使用控制器中的鍵:
$test = $this->getRequest()->getParam('test') //$test = 1234
或更短
$test = $this->_getParam('test');
因為$_GET
在查詢字符串中包含變量-這是問號后面URL的一部分。 請注意,您的.htaccess
文件中的重寫規則將所有不引用現有文件或目錄的URL都轉換為index.php
,而沒有任何原始URL的痕跡(盡管正如Gumbo的注釋提醒我,仍可以通過$_SERVER['REQUEST_URI']
訪問$_SERVER['REQUEST_URI']
。您的RewriteRule
不會創建查詢字符串(即,它們不會在URL中添加問號),這是使用$_GET
要做的。
我建議用類似以下內容替換您的上一個RewriteRule
RewriteRule ^.*$ index.php$0 [NC,L]
$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代碼來解析該代碼,並選擇所需的任何參數。
如果您不希望將開頭的/index/index
保存在path_info變量中,則可以改用這樣的RewriteRule
:
RewriteRule ^/index/index(.*)$ index.php$1 [NC,L]
要么
RewriteRule ^(/index)*(.*)$ index.php$2 [NC,L]
剝離任意數量的前導/index
es。
編輯 :實際上,您可以保留現有的RewriteRule
,只需查看$_SERVER['REQUEST_URI']
即可獲得原始請求URI; 無需弄亂路徑信息。 然后,您可以根據需要在PHP中進行拆分。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.