【问题标题】:codeigniter 2 url routing + htaccess weird querystringcodeigniter 2 url 路由 + htaccess 奇怪的查询字符串
【发布时间】:2012-08-02 17:42:08
【问题描述】:

我的 Codeigniter 路由有问题,希望有人能提供帮助。路由在大多数情况下都可以正常工作,但在涉及子域时似乎会中断。我认为这与 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。奇怪的是探查器没有拾取上面的 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,这在谷歌搜索时似乎很常见。但是我添加了尾部斜杠的代码并强制 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 应更改为 REQUEST_URI 但这也不影响它。

我还考虑过检查 uri 段。如果它存在,则只有 404,但即使该段不存在。就 CI 而言,就好像额外的字符串甚至不存在一样。

希望我没有错过任何东西。如果您需要更多示例代码,请随时说。

感谢阅读,希望有人能提供帮助并解释为什么会发生这种情况。

【问题讨论】:

    标签: php .htaccess routing query-string codeigniter-2


    【解决方案1】:

    .htaccess 文件由于 #Forces WWW 部分而强制更改 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),您可能仍会被重定向,因为您的浏览器已缓存 301。在您没有缓存的其他浏览器上进行测试使用几天,或清除当前浏览器缓存。

    **再看一眼,考虑到您已经强制执行多少重定向,尾部斜杠部分也可能会导致您的 Chrome 浏览器出现问题。我之前在 CI 中遇到过这种情况,添加/删除斜杠会导致 Chrome 将页面标记为可能是恶意的

    【讨论】:

    • 感谢您的回复和解释。我只是尝试过,只是我用 .* 替换了“子域”,因为子域是动态的。这种工作。它不再有 index.php,但仍然加载“a-page”方法而不是 404'ing。这是否意味着子域可能会导致重复的问题,因为谷歌可能会缓存一个 www 版本而没有一个?感谢您的帮助。
    • 也感谢您对 Chrome 的关注。我将对此进行测试,看看它是否可以播放:)
    • “一页”问题非常奇怪。这可能是之前 301 重定向的缓存问题,所以我会先尝试不同的浏览器,但我在您的路由或 .htaccess 文件中看不到任何应该导致此操作的内容。至于 www 缓存,如果您打算使用子域,我建议您强制不使用 www,以避免重复。
    • 感谢您的帮助。你知道我会如何强制 htaccess 中的通配符子域没有 www 吗?我想一旦我将它降低到 URL 的 1 个版本,我就可以检查它是否存在/不存在于数组中。感谢到目前为止的帮助!
    • RewriteCond %{HTTP_HOST} ^www [NC] RewriteRule ^(.*)$ http://mysite.co.uk/$1 [L,R=301]
    猜你喜欢
    • 1970-01-01
    • 2020-11-06
    • 2015-07-10
    • 1970-01-01
    • 2013-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-21
    相关资源
    最近更新 更多