2010-07-23 60 views
3

我在代碼點火器路由時遇到困難。代碼點火器重定向GET參數問題

http://www.mysite.com轉到正確的控制器並做正確的事情。但是,http://www.mysite.com/?ref=p&t2=455會導致404錯誤。另外http://www.mysite.com/mycontroller/mymethod/?ref=p&t2=455工作正常。

我更改了config.php文件中的uri_protocol並嘗試了不同的值。汽車似乎工作得最好。

我的理論是,代碼點火器使用查詢參數來做路由。問題是這些查詢參數與路由無關。

如何讓代碼點火器忽略默認控制器的查詢參數?

請注意,我按照在線說明從URL中刪除了index.php。我不認爲它造成的問題,但這裏是我的.htaccess文件,以防萬一:

RewriteEngine On 
RewriteBase /~trifecta/prod/ 

#Removes access to the system folder by users. 
#Additionally this will allow you to create a System.php controller, 
#previously this would not have been possible. 
#'system' can be replaced if you have renamed your system folder. 
RewriteCond %{REQUEST_URI} ^system.* 
RewriteRule ^(.*)$ /index.php?/$1 [L] 
#Checks to see if the user is attempting to access a valid file, 
#such as an image or css document, if this isn't true it sends the 
#request to index.php 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
#This last condition enables access to the images and css folders, and the robots.txt file 
#Submitted by Michael Radlmaier (mradlmaier) 
RewriteCond $1 !^(index\.php|images|robots\.txt|css) 
RewriteRule ^(.*)$ index.php?/$1 [L] 
+0

從'RewriteRule'線,除非你瘋狂的CGI主機或東西卸下問號。 – Zack 2010-07-23 18:49:12

回答

5

隨着該重寫規則RewriteRule ^(.*)$ /index.php?/$1,這裏是被存在的重寫的一些例子:

http://www.mysite.com =>
http://www.mysite.com/index.php?/(實際上,這可能不會被所有重寫)

http://www.mysite.com/mycontroller/mymethod/?ref=p&t2=455 =>
http://www.mysite.com/index.php?/mycontroller/mymethod/?ref=p&t2=455

http://www.mysite.com/?ref=p&ts=455 =>
http://www.mysite.com/index.php?/?ref=p&t2=455

無論它是否被重寫,第一個都會工作。 CodeIgniter正在處理一個空的查詢字符串(這很容易)或一個簡單的「/」查詢字符串。

第二個(也有效)正在被重寫,但CodeIgniter能夠處理它的查詢字符串,即/mycontroller/mymethod/?ref=p&t2=455。CI將其轉化爲一系列細分,如

[0] => mycontroller 
[1] => mymethod 
[2] => ?ref=p&t2=455 

陣列索引2最終會被您所做的任何事情忽略。 。

第三個(其中工作正在重寫,笨能不處理的查詢字符串它的查詢字符串改寫爲:/?ref=p&t2=455這使得對段的陣列看起來像這樣:

[0] => ?ref=p&t2=455 

不符合網站上的任何控制器

也許,你會從
RewriteRule ^(.*)$ /index.php?/$1到修改重寫規則修復整個事情。 10 RewriteRule ^(.*)$ /index.php/$1
此時您可能想要將uri_protocol配置更改回PATH_INFO

+0

我完全同意。該重寫規則看起來有點時髦。 – 2010-07-29 12:41:05

+0

有_some_場合您需要傳遞查詢字符串中的所有內容,但這不會適用於傳遞另一個查詢字符串。如果查詢字符串僅用於客戶端,則可以通過將一個?在重寫字符串的末尾。 – ebynum 2010-07-29 12:56:25

+0

我不得不做兩件事:改變你提到的重寫規則,但也要把uri_protocol改成ORIG_PATH_INFO。兩者都解決了這個問題 – Tihom 2010-08-02 22:37:32

0

這似乎是在笨的方式限制的設計,而不是你的路由和/或.htaccess的結果。有人提交了一個錯誤報告here。然而,而不是使用這個並添加您mymethod代碼到你的默認控制器的index方法,你可以使用_remap()功能,像這樣:

function _remap($method) 
{ 
    if ($method == 'index' && count($_GET) > 0) 
    { 
     $this->mymethod(); 
    } 
    else 
    { 
     $this->index(); 
    } 
} 
+0

從給出的描述中,作者沒有啓用查詢字符串。如果他這樣做了,則提供的示例URL將是http://www.mysite.com/?c=mycontroller&m=mymethod而不是/ mycontroller/mymethod。 – ebynum 2010-07-29 03:19:01

+0

另外,除非他做了特別特別的事情,CI清空$ _GET,所以它總是空的。 – ebynum 2010-07-29 03:21:03

+0

@ebynum:根據他的崗位,要與控制器和方法的完整URL包含一切正常:「還http://www.mysite.com/mycontroller/mymethod/?ref=p&t2=455工作正常」。但你可能是對的。 – stormdrain 2010-07-29 13:07:28

0

用下面的配置它工作正常,我。 http://www.site.com/?x=y被路由到默認控制器的索引方法。

的.htaccess

RewriteEngine on 
RewriteCond $1 !^(index\.php) 
RewriteRule ^(.*)$ index.php/$1 [L] 

系統/應用/配置/ config.php文件

$config['base_url'] = "http://www.site.com/"; 
$config['index_page'] = ""; 
$config['uri_protocol'] = "PATH_INFO"; 
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-?'; 

記得補充?到URL中允許的字符列表。也許這是造成問題的原因。我只是嘗試使用CodeIgniter的全新安裝進行設置,並且它工作正常。這是我需要做的唯一改變。

0

這裏是我周圍如何到達: 1.我在.htaccess標準重寫:

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php?/$1 [QSA,L] 

這可能是無關的,但...

  • 在我的index.php添加的應用程序文件夾以外的文件:
  • $圖案= 「/\?.*$/」; $ replacement =''; $ _SERVER [ 'REQUEST_URI'] = preg_replace函數($圖案,$替換,​​$ _SERVER [ 'REQUEST_URI']);

    這將停止笨試圖使用查詢字符串的請求。現在你真的需要$ _GET變量嗎?它們需要從$ _SERVER ['REDIRECT_QUERY_STRING']中解析出來,如果你像上面那樣使用了mod_rewrite,將會設置它。