簡體   English   中英

如何從CodeIgniter中的URL中刪除“index.php”?

[英]How do I remove 'index.php' from URL in CodeIgniter?

如何從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。 問題是什么?

.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

這100%有效,我試過了。 .htaccess文件中添加以下內容:

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

我也遇到了CodeIgniter用戶指南中建議的重寫規則的問題。 我通過從index.php前面刪除斜杠來解決它

RewriteRule ^(.*)$ index.php/$1 [L]

首先,確保已定義默認控制器 然后確保以這樣的方式構造URL ,即存在與之關聯的控制器。 例如,

主頁:

example.com/home/

或者關於我們頁面:

example.com/about/  

還要仔細檢查是否已打開mod-rewrite模塊。

我也有這個問題,發現Shane的回答幫助了我。 我在WAMP中設置了我的網站。

RewriteBase /mysite/

mysite是Apache中設置的別名。 這適用於我的所有網站。

.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'] = '';

對於我的所有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]

我希望這有幫助。

暫無
暫無

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

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