CodeIgniter删除了使用.htaccess的index.php和.php扩展名

时间:2018-06-25 16:19:27

标签: php .htaccess codeigniter mod-rewrite

在遇到“重复问题”之前,请注意,我正在询问如何使用以下命令在当前的.htaccess中添加一些mod_rewrite规则:

Options +FollowSymLinks

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

我的问题是,我的应用程序目录之外的文件没有使用Controller设置。因此,我该如何用当前的.htaccess来删除所有.php扩展名,而WHILST仍保留从URL中删除index.php(或index)参数的功能?

编辑:我已经尝试了另一个RewriteCond和RewriteRule。但是,由于CodeIgniter认为它是一个控制器,因此它会抛出404。

2 个答案:

答案 0 :(得分:2)

根据 Remove .php extension with .htaccess ,您应该添加

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]

添加到.htaccess 中,您应该在CodeIgniter规则之前添加它,例如:

Options +FollowSymLinks

RewriteEngine on

# Serve up non-CodeIgniter PHP files if they are requested without their extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]

# CodeIgniter rewrite rules
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

由于mod_rewrite按顺序处理和应用规则,因此您希望先尝试使用非CodeIgniter文件,然后再使用CodeIgniter规则。


我与该网站没有任何关系,并且我肯定还有其他类似的网站,但是如果您只想使用各种URL测试重写规则,请查看https://htaccess.madewithlove.be/

答案 1 :(得分:-1)

用于从CodeIgniter中的URL中删除index.php。您必须执行以下步骤。

1。在.htaccess中添加以下代码,并且.htaccess必须位于根文件夹中。

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

2。将$config['index_page'] = 'index.php';更改为$config['index_page'] = '';

您不需要在CodeIgniter中删除.php,因为它不存在。 如果你想学习!因此,请使用此代码删除.php扩展名 将此代码添加到.htaccess

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]