Codeigniter htaccess无法正常工作

时间:2012-04-18 12:05:14

标签: php .htaccess codeigniter

我在我的网站上使用此htaccess文件删除index.php。

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

但是这个文件不起作用。在Apache模块中启用了写法。使用的codeigniter版本是2.0.1。

6 个答案:

答案 0 :(得分:4)

我在没有codeigniter的情况下对此进行了测试,但它确实有效。

我在htdocs下创建一个名为“demo”的文件夹。我把“.htaccess”放在你的内容+附加行(RewriteBase):

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

然后下面的“index.php”:

<?php
echo $_SERVER['PATH_INFO'];

经测试: http://localhost/demo/hello

结果: /你好


要检查的事项:

  • 如果您在文件夹中放入“.htaccess”,请不要忘记添加 RewriteBase。
  • 确保为您的目录启用了FollowSymLinks。有关详细信息,请参阅this link

答案 1 :(得分:1)

阅读this

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

答案 2 :(得分:1)

对于 Codeigniter 3

来说,这可能是很晚的解决方案

将此内容放在您的根文件夹中

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

转到apache / apache_version / conf / httpd.conf(我使用Laragon)

放在httpd.conf的末尾

Listen 9595
<VirtualHost *:9595>
DocumentRoot "C:\laragon\www\capstone_project"
<Directory "C:\laragon\www\capstone_project">
Order allow,deny
Allow from all
AllowOverride All
</Directory>
</VirtualHost>

重新启动您的Apache,看看它是否有效。

答案 3 :(得分:0)

将其复制到.htaccess

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /CI_FOLDER/
#CI_FOLDER is the Location of your CI files.

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

答案 4 :(得分:0)

将此代码放入CI_Folder的.htaccess文件

(2048/2)**2

答案 5 :(得分:0)

检查您的Apache站点配置文件。在Ubuntu 14.04上,该文件为/etc/apache2/sites-available/site.conf

更改此行

AllowOverride None

到此

AllowOverride All

重新启动Apache以加载更新的site.conf

sudo service apache2 restart

这解决了我。