首页 文章

CodeIgniter仅显示默认控制器

提问于
浏览
7

我在Local WAMP 中使用Codeigniter . 代码工作正常 . 但我上传到Cpanel(在example.com内部,文件夹名称调用' mysite ') . 我改变了,

  • db_name(config / database.php)

  • db_user_name(config / database.php)

  • db_password(config / database.php)

  • base_url as http://example.com/mysite(config / config.php)

  • uri_protocol as REQUEST_URI(config / config.php)

并且还改变了 .htaccess (mysite / .htaccess),

<IfModule mod_rewrite.c>

RewriteEngine On

# Set the rewritebase to your CI installation folder
RewriteBase /mysite/


# Send everything to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

但不是在mysite / application / .htaccess中 . 它是空的 .

问题是,如果我去http://example.com/mysite,它正确显示默认页面 . 但如果我点击任何链接(http://example.com/mysite/user/signin),它显示相同的默认页面 . 但URL已更改 .

请帮帮我...

config.php

$config['base_url'] = 'http://example.com/mysite';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
$config['url_suffix'] = '';
$config['language'] = 'english';
$config['charset'] = 'UTF-8';
$config['enable_hooks'] = FALSE;
$config['subclass_prefix'] = 'MY_';
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-';
$config['allow_get_array']      = TRUE;
$config['enable_query_strings'] = FALSE;
$config['controller_trigger']   = 'c';
$config['function_trigger']     = 'm';
$config['directory_trigger']    = 'd';
$config['log_threshold'] = 0;
$config['log_path'] = '';
$config['log_date_format'] = 'Y-m-d H:i:s';
$config['cache_path'] = '';
$config['encryption_key'] = '***';
$config['sess_cookie_name']     = 'ci_session';
$config['sess_expiration']      = 7200;
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie']  = FALSE;
$config['sess_use_database']    = FALSE;
$config['sess_table_name']      = 'ci_sessions';
$config['sess_match_ip']        = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update']  = 300;
$config['cookie_prefix']    = "";
$config['cookie_domain']    = "";
$config['cookie_path']      = "/";
$config['cookie_secure']    = FALSE;
$config['global_xss_filtering'] = FALSE;
$config['csrf_protection'] = FALSE;
$config['csrf_token_name'] = 'csrf_test_name';
$config['csrf_cookie_name'] = 'csrf_cookie_name';
$config['csrf_expire'] = 7200;
$config['compress_output'] = FALSE;
$config['time_reference'] = 'local';
$config['rewrite_short_tags'] = FALSE;
$config['proxy_ips'] = '';
define('CSS_FOLDER' , 'application/assets/css');
define('DEFAULT_IMAGE_URL' , 'application/assets/images/default');

routes.php

$route['default_controller'] = "welcome";
$route['404_override'] = '';
$route['user/(:any)'] = 'user/index';

15 回答

  • 1

    去掉

    $route['user/(:any)'] = 'user/index';
    

    在routes.php文件中 . user / signin匹配上述条件,这就是CI将请求路由到user / index而不是“User”控制器“signin”函数的原因 .

    您想通过该路由配置实现什么目标?

  • 1

    可以告诉CodeIgniter在URI不存在时加载默认控制器,就像只请求您的站点根URL一样 . 所以一旦用routes.php中的conrtoller替换你的默认控制器'welcome',它可能会有帮助您

  • 1
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    
  • 0

    试试这个htaccess:

    RewriteEngine on
    RewriteCond $1 !^(index\.php|images|robots\.txt)
    RewriteRule ^(.*)$ /mysite/index.php/$1 [L]
    

    你也应该创建一个名为user的控制器,这个控制器应该是函数登录 .

  • 1

    我使用了以下.htaccess文件,它适用于cpanel .

    # index file can be index.php, home.php, default.php etc.
    DirectoryIndex index.php
    
    # Rewrite engine
    RewriteEngine On
    
    # condition with escaping special chars
    RewriteCond $1 !^(index\.php|robots\.txt|favicon\.ico)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
    
  • 1
    $config['base_url'] = 'http://example.com/mysite/';
    

    比以下更好:

    $config['base_url'] = 'http://example.com/mysite';
    

    试试这个 :

    RewriteBase /
    
    RewriteEngine on
    RewriteCond $1 !^(index\.php|images|robots\.txt)
    RewriteRule ^(.*)$ /mysite/index.php/$1 [L]
    
  • 1

    我正面临同样的问题..当我使用时解决了

    $config['uri_protocol'] = 'AUTO';
    

    在config.php ..try中使用AUTO和PATH_INFO相同

    确保在apache中启用了rewrite_module

  • 1

    如果您打算在服务器上使用子文件夹,请在本地WAMP上执行相同操作 . 这样,您可以在将其发送到服务器之前对其进行测试 .

    我很久以前使用CI,但我认为你必须在一些配置文件中添加 mysite/ .

    见:Installing a CodeIgniter application in a subfolder

  • 0

    .htaccess代码

    将默认配置恢复为config.php,希望这对您有所帮助 .

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

    打开config.php并执行以下替换

    $config['index_page'] = "index.php"
    to
    $config['index_page'] = ""
    

    在某些情况下,uri_protocol的默认设置无法正常工作 . 只需用$ config ['uri_protocol'] =“REQUEST_URI”替换$ config ['uri_protocol'] =“AUTO”

    .htaccess的

    RewriteEngine on
    RewriteCond $1 !^(index\.php|resources|robots\.txt)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L,QSA]
    
  • 3

    有时“ORIG_PATH_INFO”修复此问题 . 所以您也可以尝试将uri_protocol更改为“ORIG_PATH_INFO”

    $config['uri_protocol'] = "ORIG_PATH_INFO";
    

    并且base_url应该是绝对的,

    $config['base_url'] = "http://example.com/mysite/";
    
  • 1

    不要在.htaccess文件中写任何内容并删除$ route ['user /(:any)'] ='user / index';线和一件非常重要的事情确保在你的服务器mod重写扩展我应该启用 .

    这可以帮你How to enable mod_rewrite in php.ini on shared hosting

  • 6

    我通过将控制器文件名更改为以大写字母开头来解决了同样的问题 .

    就像下载的默认“欢迎”控制器一样 .

  • 1

    你必须要考虑的事情很少 . 当您使用codigniter框架设置项目时 . 以下是您必须进行更改的文件 .

    1).htaccess:

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /mysite/index.php/$1 [L]
    

    2)application/config/config.php

    $config['base_url'] = 'http://example.com/mysite/';
    $config['server_root'] = $_SERVER['DOCUMENT_ROOT'];
    $config['index_page'] = '';
    $config['uri_protocol'] = 'AUTO';
    

    3)application/config/database.php

    $db['default']['hostname'] = 'localhost'; // set your hostname
    $db['default']['username'] = 'root'; // set database username
    $db['default']['password'] = 'root'; //set database password
    $db['default']['database'] = 'XYZ'; //set your database name
    $db['default']['dbdriver'] = 'mysql';
    

    4)application/config/routes.php

    $route['default_controller'] = "user";  //your default controller
    $route['404_override'] = '';
    

    Note: 您的 .htaccess 文件必须位于根目录中 . 这里它应该在 mysite 文件夹中

  • 1

    试试这个

    $config['uri_protocol'] = 'PATH_INFO';
    $config['enable_query_strings'] = FALSE;
    

    Httaccess

    <IfModule mod_rewrite.c>
    RewriteEngine On
    
    #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 [NC]
    </IfModule>
    

相关问题