簡體   English   中英

CodeIgniter-使用.htaccess刪除的index.php和.php擴展名

[英]CodeIgniter - removed index.php AND .php extension using a .htaccess

在被“重復問題”打招呼之前,請注意,我正在詢問如何使用以下命令在當前的.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擴展名,同時保持從URL刪除index.php(或index)參數的能力?

編輯:我嘗試了另一個RewriteCond和RewriteRule。 但是,CodeIgniter會拋出404,因為它認為它是控制器。

根據刪除帶有.htaccess的.php擴展名,您應該添加

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/

用於從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]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM