2012-08-02 62 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