CodeIgniter仅显示默认控制器

时间:2013-07-16 12:47:22

标签: php .htaccess codeigniter

我在本地 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为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 个答案:

答案 0 :(得分:6)

$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 :(得分:3)

我面临同样的问题..当我使用

时解决了
$config['uri_protocol'] = 'AUTO'; 
在config.php ..try中使用AUTO和PATH_INFO进行相同的

确保在apache中启用了rewrite_module

答案 2 :(得分:2)

.htaccess Code

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

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

答案 3 :(得分:1)

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

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

请参阅:Installing a CodeIgniter application in a subfolder

答案 4 :(得分:1)

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

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

和 base_url应该是绝对的,

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

答案 5 :(得分:1)

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

答案 6 :(得分: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>

答案 7 :(得分:1)

试试这个htaccess:

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

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

答案 8 :(得分: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]

答案 9 :(得分:1)

删除

$route['user/(:any)'] = 'user/index';
在routes.php文件中

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

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

答案 10 :(得分:1)

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

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

在某些情况下,uri_protocol的默认设置无法正常运行。 只需更换     $ config ['uri_protocol'] =“自动”     通过     $ config ['uri_protocol'] =“REQUEST_URI”

htaccess的

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

答案 11 :(得分:1)

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

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

答案 12 :(得分:1)

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

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

答案 13 :(得分:0)

您必须考虑很少的事情。当您使用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'] = '';  

注意:您的 .htaccess 文件必须位于根目录中。这里应该是 mysite 文件夹

答案 14 :(得分:0)

试试这个

$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>