2012-08-02 106 views
0

我的Codeigniter路由有問題,希望有人可以幫忙。大多數情況下,路由工作正常,但在涉及子域時似乎會中斷。我認爲它與htaccess有關,但我真的不確定,希望有人能夠提供幫助。codeigniter 2 url routing + htaccess奇怪的查詢字符串

如果我去mysite.co.uk/a-page/這按預期工作。

如果我去subdomain.mysite.co.uk這也適用(我有一個單獨的default_controller爲子域加載)。

如果有人去subdomain.mysite.co.uk/anything 404這是預期的。

但如果有人去subdomain.mysite.co.uk/a-page/這也應該404但它不。相反,它更改爲以下URL:

http://www.subdomain.mysite.co.uk/index.php?/a-page/ 

然後加載「a-page」功能。我希望它達到404.奇怪的是,profiler並沒有拿起上面的URI。相反,它只是認爲它是「一頁」。

在我的配置我有以下幾點:

$config['index_page'] = ''; 
$config['uri_protocol'] = 'AUTO'; 
$config['url_suffix'] = '/'; 
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-'; 

我知道它的不理想,但爲子域檢查我做我的routes.php文件

if(isset($_SERVER['HTTP_HOST'])){ 
    $currentURL = parse_url('http://' . $_SERVER['HTTP_HOST']); 
    $urlHostPieces = explode('.', $currentURL['host']); 
    $subdomainValue = $urlHostPieces[1]; // will either be a genuine value or the domain name 
}else{ 
    $subdomainValue = DOMAIN; 
} 

if($subdomainValue !== DOMAIN){ 
    //is subdomain 
    $route['default_controller'] = "controller/function/$subdomainValue"; 
} else{ 
    //everything else 
    $route['default_controller'] = "controller/home"; 
} 

我的底部以下我使用的是Fabdrol的htaccess,這在Google搜索時似乎很常見。不過,我添加了一個斜線後的代碼並強制www。

<IfModule mod_rewrite.c> 
    RewriteEngine On 
    RewriteBase/

    #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] 

    #When your application folder isn't in the system folder 
    #This snippet prevents user access to the application folder 
    #Submitted by: Fabdrol 
    #Rename 'application' to your applications folder name. 
    RewriteCond %{REQUEST_URI} ^application.* 
    RewriteRule ^(.*)$ /index.php?/$1 [L] 

    # this adds trailing slash 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_URI} !(.*)/$ 
    RewriteRule ^(.*)$ $1/ [R=301,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 
    RewriteRule ^(.*)$ index.php?/$1 [L] 

    # Forces WWW 
    RewriteCond %{HTTP_HOST} !^www\. 
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] 


</IfModule> 

<IfModule !mod_rewrite.c> 
    # If we don't have mod_rewrite installed, all 404's 
    # can be sent to index.php, and everything works as normal. 

    ErrorDocument 404 /index.php 
</IfModule> 

任何想法如何我可以404奇怪的變化?還要讀取uri_protocol應該chnaged到REQUEST_URI,但是這也沒有影響它。

另外我想過檢查uri段。如果它存在,那麼只是404,但即使該段不存在。就像CI一樣,額外的字符串甚至不存在。

希望我沒有遺漏任何東西。如果你需要更多的示例代碼,可以隨意說。

感謝您閱讀並希望有人能夠幫助並解釋爲什麼會發生這種情況。

回答

1

由於#Forces WWW部分導致您的子域路由中斷,.htaccess文件強制更改URL。 (我假設)

嘗試改變線路:

# Forces WWW 
RewriteCond %{HTTP_HOST} !^www\. 
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] 

要:

# Forces WWW 
RewriteCond %{HTTP_HOST} !^(www|subdomain)\. 
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] 

此外,因爲您正在使用301個重定向(你要改變那些R = 302 for Dev Environments),您可能仍會被重定向,因爲您的瀏覽器已緩存了301.在另一個瀏覽器上測試了幾天未使用過的瀏覽器,或清除了當前的瀏覽器緩存。

**只需再看一眼,尾部的斜槓部分也可能導致您在Chrome瀏覽器中遇到問題,因爲您已經有多少次重定向。我之前在CI中遇到過這種情況,在其中添加/刪除尾部斜槓導致Chrome將頁面標記爲可能是惡意的

+0

感謝您的回覆和解釋。我剛剛嘗試過,只是我用。*替換了「子域」,因爲子域是動態的。這種工作。它沒有index.php了,但仍然加載「a-page」方法而不是404'。這也意味着子域名可能會導致重複問題,因爲Google可能會緩存一個www版本和一個沒有?謝謝你的幫助。 – fl3x7 2012-08-02 18:18:48

+0

非常感謝Chrome上的領導者。我會測試這個,看看它是否起到:) – fl3x7 2012-08-02 18:21:12

+0

'一頁'的問題是非常奇怪的。這可能是先前301重定向的緩存問題,所以我會先嚐試其他瀏覽器,但在路由或.htaccess文件中看不到任何應該導致此操作的東西。 至於www緩存,如果你打算使用子域名,我會建議強制不使用www域名,以避免重複。 – BayssMekanique 2012-08-02 18:34:49