使用.htaccess文件删除CodeIgniter中的index.php
我目前正在使用CodeIgniter开发一个网站,最近我偶然发现了一个路由和.htaccess问题 基本上,我的简单网站的结构(为了简单起见,我们称我的项目为“CIExample”)如下所示: -主页使用.htaccess文件删除CodeIgniter中的index.php,.htaccess,codeigniter,codeigniter-routing,.htaccess,Codeigniter,Codeigniter Routing,我目前正在使用CodeIgniter开发一个网站,最近我偶然发现了一个路由和.htaccess问题 基本上,我的简单网站的结构(为了简单起见,我们称我的项目为“CIExample”)如下所示: -主页 -关于我们 -我们的服务 -新闻 -联系我们 这是我用页面控制器实现的。该控制器有5个功能,分别调用相应页面的视图,即: Home(),它调用视图“Home” About(),它调用视图“About Us”,依此类推 以前,要访问“主页”,我需要键入http://localhost/CIExamp
-关于我们
-我们的服务
-新闻
-联系我们 这是我用页面控制器实现的。该控制器有5个功能,分别调用相应页面的视图,即: Home(),它调用视图“Home”
About(),它调用视图“About Us”,依此类推 以前,要访问“主页”,我需要键入
http://localhost/CIExample/index.php/page/home
进入url窗口,但在设置以下路由后,我能够删除“页面”(类名)部分:
然而,当我试图删除'index.php'时,棘手的部分出现了。
我希望能够通过键入http://localhost/CIExample/home
因此,我在CI forum/tutorial/stack overflow上进行了大量搜索,并找到了一些.htaccess文件的代码:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
或我尝试了两种代码,但都不起作用
http://localhost/CIExample/home
将引导我找到404未找到页面,但http://localhost/CIExample/index.php/home
就可以了
我想知道出了什么问题?是my routes.php还是.htaccess,还是两者都有?
谢谢
注意:我还更改了我的'config/config.php'文件->$config['index\u page']=''代码>
编辑:
终于成功了!
调整config文件夹中的config.php文件并设置以下$config['uri_protocol']='PATH_INFO'代码>(来自“自动”)
可用值:
| 'AUTO' Default - auto detects
| 'PATH_INFO' Uses the PATH_INFO
| 'QUERY_STRING' Uses the QUERY_STRING
| 'REQUEST_URI' Uses the REQUEST_URI
| 'ORIG_PATH_INFO' Uses the ORIG_PATH_INFO
不知道有什么不同,但根据中的一条注释,相同的代码可能/可能不适用于生产服务器
任何人都可以解释原因吗?我所有的CodeIgniter项目都使用这个htaccess。它也支持子文件夹
<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]
#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>
重新启动发动机
重写基/
#删除用户对系统文件夹的访问权限。
#此外,这将允许您创建System.php控制器,
#以前这是不可能的。
#如果已重命名系统文件夹,则可以替换“系统”。
重写COND%{REQUEST_URI}^系统*
重写规则^(.*)$/index.php?/$1[L]
#当应用程序文件夹不在系统文件夹中时
#此代码段阻止用户访问应用程序文件夹
#提交人:Fabdrol
#将“应用程序”重命名为应用程序文件夹名称。
重写cond%{REQUEST_URI}^应用程序*
重写规则^(.*)$/index.php?/$1[L]
#检查用户是否试图访问有效文件,
#例如图像或css文档,如果这不是真的,它会发送
#请求index.php
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
重写规则^(.*)$index.php?/$1[L]
#如果我们没有安装mod_rewrite,所有404
#可以发送到index.php,一切正常。
#提交人:ElliotHaughin
ErrorDocument 404/index.php
您只需将其粘贴到.htaccess文件中:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
我已经使用了下面的方法,它在UBUNTU中运行得非常好
首先,验证您在index.php文件中是否启用了mod_rewrite(可以使用phpinfo()
进行验证)
如果禁用mod_rewrite,则运行:
1. <代码>sudo a2enmod重写
2. <代码>sudo gedit/etc/apache2/sites available/defaut
只需将标记()中的“AllowOverride None”替换为“AllowOverride All
”
3.然后请运行sudo服务apache2重新启动
好的,很好。。。
现在,在Codeigniter的根目录中->查找.htaccess
文件,如果未找到,请按ctrl+h,仍然未找到,然后创建一个文件.htaccess
在.htaccess
粘贴以下代码:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /ROOT DIRECTORY NAME/
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
重新启动发动机
重写基本/根目录名/
#通过站点索引文件指示所有EE web请求
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
重写规则^(.*)$index.php/$1[L]
好的,最后只需替换根目录名
不要忘记从application/config/config.php
文件中删除index.php
把那条线画得像
$config['index_page']=''代码>用于代码点火器3.1.4
#php——开始cPanel生成的处理程序,不编辑
#将“ea-php56”包设置为默认的“PHP”编程语言。
AddType应用程序/x-httpd-ea-php56.php.php5.phtml
#php——结束cPanel生成的处理程序,不编辑
#开始cPanel生成的php ini指令,不编辑
#手动编辑此文件可能会导致意外行为。
#要对此文件进行更改,请使用cPanel MultiHP INI编辑器(主页>>软件>>MultiHP INI编辑器)
#有关更多信息,请阅读我们的文档(https://go.cpanel.net/EA4ModifyINI)
上的php_标志asp_标记
php_标志显示上的错误
php_值最大执行时间30
php_值最大输入时间60
php_值最大输入值为1000
php_值内存_限制512M
php_值post_最大尺寸128M
php_value session.gc_maxlifetime 1440
php_value session.save_path“/var/cpanel/php/sessions/ea-php56”
php\u值上传\u最大\u文件大小128M
php_标志zlib.output_压缩关闭
#END cPanel生成的php ini指令,不编辑
重新启动发动机
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
重写规则^(.*)$index.php/$1[L,QSA]
本地:
#RewriteRule .* index.php/$0 [PT,L]
在系统/应用程序/配置/配置.php
中,更改
$config['index_page'] = "index.php";
到
在.htaccess中,添加以下内容:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|stylesheets|scripts|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
来自CI论坛的代码应该可以工作。我就是这么用的。你可能需要定义你的基地,因为你在一个潜艇
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
#RewriteRule .* index.php/$0 [PT,L]
$config['index_page'] = "index.php";
$config['index_page'] = "";
RewriteEngine on
RewriteCond $1 !^(index\.php|images|stylesheets|scripts|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]