[英]CodeIgniter removing index.php not working
我將 Ubuntu 13 與以下設置用於本地 codeigniter 站點。
Apache/2.4.6 (Ubuntu)
5.5.3-1ubuntu2.2
'CI_VERSION', '2.1.2'
如果沒有index.php
,URL 將不再工作。 它們曾經可以工作,但是在從Ubuntu 12.x
升級到13.x
並在過去一年中進行了一些 apache 更新后, localhost
站點不再正常工作。
如果我去localhost/index.php/controllername/
它工作但如果我去 localhost/controllername/ 它不。
mod_rewrite
已啟用。
CodeIgniter 配置有:
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO'; // tried all available options here and
沒有任何效果
在域的.conf
文件中,我有這個:
<Directory />
Options -Multiviews +FollowSymLinks
AllowOverride All
</Directory>
這里的.htaccess
文件注釋行是我嘗試過但不起作用的行。
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# RewriteCond $1 !^(index\.php|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
# RewriteRule .* index.php/$0 [PT,L]
# RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
我在谷歌上搜索並閱讀了我能找到的所有內容,並嘗試了我能找到的所有內容,包括 Stack Overflow 上的幾篇文章,包括“可能已經有答案的問題”中的文章。 似乎仍然沒有任何效果。 但就像我說的,這在過去有效,但只有在對操作系統和 Apache 進行多次更新后,我才第一次注意到它停止工作。
我將在未來的項目中遠離 CodeIgniter,但這些項目已經存在。 對可能是什么問題感到困惑。
解決方案:
事實證明,這根本不是 codeigniter 問題。 這是一個 apache 問題,但與重寫規則無關。 在我的 apache2.conf 中,我必須更改 /var/www/ 的塊
要求所有授予似乎都成功了。
DirectoryIndex index.php index.html Options Indexes FollowSymLinks AllowOverride All Require all granting
只是為了更好的衡量,我也在這里進行了更改:Options FollowSymLinks AllowOverride All Require all allowed
在 askubuntu 上找到https://askubuntu.com/questions/421233/enabling-htaccess-file-to-rewrite-path-not-working
將以下代碼復制到根文件夾中的.htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
Options All -Indexes
這對我來說很有效,可以刪除 CodeIgniter 中的 index.php。
CodeIgniter 似乎沒有默認的.htaccess
,所以不清楚它來自哪里。 但出於調試目的,我建議您執行以下操作。 首先,這是您的.htaccess
全部清理完畢:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
現在用這個替換你的index.php
。 它只是轉儲通過.htaccess
傳遞的$_GET
值,如下所示:
echo '<pre>';
print_r($_GET);
echo '</pre>';
現在,在您的.htaccess
加載相關站點/應用程序/項目的索引頁面。 輸出應該是這樣的:
Array
(
[/controllername/here/] =>
)
這似乎是正確的。 但同樣,你會比我們更了解。
這樣做的目的是評估問題是否在於 Apache 將正確的$_GET
值傳遞給您的 CodeIgniter 設置,這是一個問題。 或者問題是否在您的 CodeIgniter 控制器邏輯本身內。
我的直覺告訴我,問題是在你的代碼庫的笨邏輯,因為從升級Ubuntu 12.x
到Ubuntu 13.x
包括升級版的PHP
從版本5.3
中Ubuntu 12.x
到版本5.5
中Ubuntu 13.x
。 我使用了許多在PHP 5.3
和PHP 5.4
中運行的代碼,但在PHP 5.5
中有時會中斷,因為該代碼庫中有重大更改,如果您不繼續使用未折舊的函數等會導致代碼中斷.
我會嘗試類似的東西:
RewriteRule ^(?!index)(.*)$ index.php?/myvariable=$1 [L]
如果您知道腳本在localhost/controllername
對controllername
期望的 GET 變量,則在規則myvariable
替換myvariable
變量名。
將 .htaccess 文件放在項目文件夾中,如下所示:
htdocs/your_project/.htaccess
試試這個 .htaccess 的新代碼:
RewriteEngine on
RewriteBase /your-project-directory-name/
RewriteCond $1 !^(index.php|resources|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.