如何从CodeIgniter中的URL删除index.php
?
我从配置文件中删除了index.php
,我在Apache(2.2.11)中运行了rewrite_module
,我的.htaccess文件是:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|captcha|css|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
现在,每当我点击任何链接时,都会显示找不到该URL。有什么问题?
答案 0 :(得分:4)
在.htaccess文件中试试这个:评论应该有助于为您调整...
这在几个CodeIgniter网站安装中对我来说非常有效。此外,如果未安装mod_rewrite,它会将您发送到404页面(或者您可以将其更改为适合您的目的)。
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
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
#This last condition enables access to the images and css folders, and the robots.txt file
RewriteCond $1 !^(index\.php|(.*)\.swf|images|robots\.txt|css|docs|cache)
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.
ErrorDocument 404 /application/errors/404.php
</IfModule>
检查您的虚拟主机文件并查看是否设置了这些项目 - 这些可能允许您的.htaccess文件正确重写URL:
Options FollowSymLinks
AllowOverride All
Order deny,allow
Deny from all
答案 1 :(得分:3)
这100%有效,我试过了。在.htaccess文件中添加以下 以下内容:
RewriteEngine On
RewriteCond $1 !^(index\.php|(.*)\.swf|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
答案 2 :(得分:2)
我也遇到了CodeIgniter用户指南中建议的重写规则的问题。我通过从index.php
前面删除斜杠来解决它RewriteRule ^(.*)$ index.php/$1 [L]
答案 3 :(得分:0)
首先,确保您已定义default controller。然后确保以这样的方式构造URLs,使得与它们相关联的控制器。例如,
首页:
example.com/home/
或关于我们页面:
example.com/about/
同时仔细检查您是否已打开mod-rewrite模块。
答案 4 :(得分:0)
答案 5 :(得分:0)
.htacess
RewriteEngine on
#Any HTTP request other than those for index.php,
#assets folder, files folder and robots.txt
#is treated as a request for your index.php file.
RewriteCond $1 !^(index\.php|assets|files|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
之后转到application/config/config.php
并使用下面提到的值修改这两个变量。
$config['base_url'] = '';
和
$config['index_page'] = '';
答案 6 :(得分:0)
对于我所有的Codeigniter项目,我使用下面的.htaccess代码:
RewriteEngine on
RewriteCond $1 !^(index.php|resources|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
我希望这会有所帮助。