后端工作正常,但前端只有默认控制器工作,如果我调用默认控制器,写控制器和函数的名称,那么它不工作
(给出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'; }
}
try this
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
From post: https://stackoverflow.com/questions/17330398/codeigniter-and-godaddy-default-controller-issues
在您的文件中,您有:
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
。