【问题标题】:On godaddy server frontend not working in CodeIgniter在 Godaddy 服务器前端无法在 CodeIgniter 中工作
【发布时间】:2015-05-20 10:11:47
【问题描述】:

后端工作正常,但前端只有默认控制器工作,如果我调用默认控制器写入控制器和函数的名称然后它不工作

(给出 404 错误“在此服务器上找不到 indialive_today/home/。”)

.htaccess

  <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /demo/indialive_today
    #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 ^(.*)$ admin/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]
    #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]
</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.
    # Submitted by: ElliotHaughin
    ErrorDocument 404 /index.php
</IfModule>

routes.php

$usertype=explode("/",$_SERVER["REQUEST_URI"]);   
require_once( BASEPATH .'database/DB'. EXT );
    $db =& DB();
    $query = $db->get( 'news' );
    $result = $query->result();  
if($usertype[3] == ''){ $route['default_controller'] = "home/index"; }
 else if($usertype[3] != ''){ foreach( $result as $row ){$route[$row->slug] = 'news_detail';    }
}   

【问题讨论】:

  • 据我所知,godaddy 共享服务器不允许您的本地 .httacess 文件。您可以要求他们解决它。或者您可以使用 /index.php/ 使用您的网址
  • 如果您在 URL 中包含 index.php,您的应用程序是否正常工作?
  • 前端工作正常问题
  • 如果您在 URL 中包含 index.php,您的应用程序是否正常工作?无法确定应该显示什么。路由文件中没有指定默认路由。

标签: php .htaccess codeigniter routes server


【解决方案1】:

试试这个

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]

来自帖子:https://stackoverflow.com/questions/17330398/codeigniter-and-godaddy-default-controller-issues

【讨论】:

    【解决方案2】:

    在您的文件中,您有:

    RewriteRule ^(.*)$ index.php?/$1 [L]
    

    看到问号了吗?这意味着您将请求传递给查询字符串 (QUERY_STRING)。但是,您正在检查请求 URI (REQUEST_URI)。

    所以,首先,您需要将该行更改为:

    RewriteRule ^(.*)$ index.php/$1 [L]
    

    一个更好的选择是这样做:

    RewriteRule ^ index.php [L]
    

    您还需要在RewriteBase 中添加正斜杠:

    RewriteBase /thinksoft/demo/indialive_today/
    

    经过一些优化后,您的文件(应该存在于/thinksoft/demo/indialive_today/ 目录中)应如下所示:

    <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.
        # Submitted by: ElliotHaughin
        ErrorDocument 404 /index.php
    
        # If you are using Apache 2.4.4 and later you can uncomment
        # the line below, which is a decent alternative for using
        # mod_rewrite
        # FallbackResource /thinksoft/demo/indialive_today/index.php
    </IfModule>
    
    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /thinksoft/demo/indialive_today/
    
        # 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 ^(.*)$ admin/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]
    
        # 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]
        # or use the following as a replacement to the rule above:
        # RewriteRule ^ index.php [L]
    </IfModule>
    

    注意:我还添加了FallbackResource 选项(已注释掉),它非常有用。

    还要确保您的 CodeIgniter 配置设置为使用 REQUEST_URI 而不是 QUERY_STRING。

    【讨论】:

    • 同样的问题只有默认控制器工作如果我在地址栏中写控制器名称它会在前端给出 404 错误但在后端工作正常
    • 您获得的是 Apache 404 还是 CI 404?
    • 未找到 在此服务器上未找到请求的 URL /thinksoft/demo/indialive_today/news1。此外,在尝试使用 ErrorDocument 处理请求时遇到 404 Not Found 错误。 Apache 服务器位于 www.thinksoftsolutions.com 端口 80
    • 这是你的 RewriteBase 错误 - 你之前没有提到 thinksoft 也是 URI 的一部分。答案已更新。
    猜你喜欢
    • 2018-03-14
    • 1970-01-01
    • 2019-08-04
    • 2015-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-29
    • 1970-01-01
    相关资源
    最近更新 更多