2013-03-18 37 views
3

刪除的index.php CI中我使用笨目前正在開發一個網站,我最近在一個路由&的.htaccess問題絆倒了。使用.htaccess文件

基本上,我簡單的網站結構(我們稱之爲我爲簡單起見項目「CIExample」)是如下:

- 首頁
- 關於我們
- 我們的服務
- 新聞
- 聯繫我們

我使用頁面控制器實現。該控制器具有5個功能,要求各網頁的觀點,即:

首頁(),它調用視圖「家」
關於(),它調用視圖「關於我們」等..

此前,訪問「家」,我需要http://localhost/CIExample/index.php/page/home鍵入的URL窗口,但設置了以下路線後,我才得以刪除「頁」(類名)部分:

$route['default_controller'] = 'page/home'; 
$route['home'] = 'page/home'; 
$route['about'] = 'page/about'; 
$route['service'] = 'page/service'; 
$route['news'] = 'page/news'; 
$route['contact'] = 'page/contact'; 

然而,當我試圖刪除'index.php'時,會出現一些棘手的部分。 我希望能夠通過鍵入http://localhost/CIExample/home訪問主頁。

所以,我做了很多CI論壇/教程/堆棧溢出的搜索,並發現.htaccess文件的一些代碼:

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

http://ellislab.com/forums/viewthread/197675/#929904
我想這兩個代碼,但沒有工作..

http://localhost/CIExample/home將指導我未找到404頁,但http://localhost/CIExample/index.php/home會工作得很好。

我不知道哪裏出了冤屈?是我的routes.php或.htaccess或兩者? 謝謝。

注:我也改變了我的 '配置/ config.php文件' 文件 - >$config['index_page'] = '';

編輯: 最後,它的工作原理! 調整在config文件夾config.php文件和設置後,以下$config['uri_protocol'] = 'PATH_INFO';(從「AUTO」)。

可選值:

| 'AUTO'   Default - auto detects 
| 'PATH_INFO'  Uses the PATH_INFO 
| 'QUERY_STRING' Uses the QUERY_STRING 
| 'REQUEST_URI'  Uses the REQUEST_URI 
| 'ORIG_PATH_INFO' Uses the ORIG_PATH_INFO 

說不上有什麼不同,但根據http://www.jotorres.com/2011/12/removing-index-php-from-url-in-codeigniter/一個評論,同樣的代碼可能/可能不會在生產服務器工作。

任何人都可以或許說明理由?

+0

來自CI論壇的代碼應該可以工作。這就是我使用的。您可能需要定義基礎,因爲您位於子目錄中 - RewriteBase/CIExample/ – 2013-03-18 16:46:49

+0

請標記幫助您的答案。它有助於作者和觀衆。 – 2015-10-19 15:41:19

回答

1

我使用這個htaccess的爲我所有的笨項目。它也支持子文件夾。

<IfModule mod_rewrite.c> 
    RewriteEngine On 
    RewriteBase/

    #Removes access to the system folder by users. 
    #Additionally this will allow you to create a System.php controller, 
    #previously this would not have been possible. 
    #'system' can be replaced if you have renamed your system folder. 
    RewriteCond %{REQUEST_URI} ^system.* 
    RewriteRule ^(.*)$ /index.php?/$1 [L] 

    #When your application folder isn't in the system folder 
    #This snippet prevents user access to the application folder 
    #Submitted by: Fabdrol 
    #Rename 'application' to your applications folder name. 
    RewriteCond %{REQUEST_URI} ^application.* 
    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 
    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. 
    # Submitted by: ElliotHaughin 

    ErrorDocument 404 /index.php 
</IfModule> 
+0

是的,這個也適用於我調整config.php文件在config文件夾(見上面的編輯)。但是,謝謝隊友:) – Stan 2013-03-19 03:52:14

12

你只需要將它粘貼到你的。htaccess文件:

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

,使這個更替到您的application/config/config.php文件:

$config['index_page'] = ''; 
+0

這解決了我的問題。 – 2014-04-11 07:58:03

+0

這也解決了我的問題。謝謝! – Katzumi 2014-06-06 11:33:08

2

我有使用下面的方法,它是在UBUNTU正常使用...! !

首先驗證您的index.php文件中是否啓用了mod_rewrite(您可以使用phpinfo()進行驗證)。

如果禁用了mod_rewrite然後運行: 1. sudo a2enmod rewrite 2. sudo gedit /etc/apache2/sites-available/defaut 只需更換有 「AllowOverride無」 到 「AllowOverride All」 的標籤(< '目錄/ var/WWW /'>) 3.然後請運行sudo service apache2 restart

好啊好啊...... 現在笨的根目錄 - >查找.htaccess文件,如果沒有找到,然後按CTRL + H,仍然沒有找到,然後創建一個文件.htaccess

.htaccess 粘貼下面的代碼:

<IfModule mod_rewrite.c> 
     RewriteEngine On 
     RewriteBase /ROOT DIRECTORY NAME/ 

     # Directs all EE web requests through the site index file 
     RewriteCond %{REQUEST_FILENAME} !-f 
     RewriteCond %{REQUEST_FILENAME} !-d 
     RewriteRule ^(.*)$ index.php/$1 [L] 
</IfModule> 

好最後只需更換ROOT DIRECTORY NAME

DONT忘記REMOVEindex.phpapplication/config/config.php文件 做出這樣 $config['index_page'] = '';

-1

該行你只需要將其粘貼到.htaccess文件中(pr oject目錄/的.htaccess):

注: 之前進行任何更改,請檢查是否「mod_rewrite的」啓用與否,如果沒有啓用試@ IRFU的方式來啓用它。

2.確保這是你的項目的根目錄內的文件中添加以下代碼,但沒有一個是已經存在,即應用/的.htaccess

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

和如上面提到的答案不這樣交替忘記您的的application/config/config.php文件文件:

$config['index_page'] = ''; 
+0

您對上述答案沒有任何區別。請不要那樣做。 – MarcoZen 2017-06-18 07:50:39

0
for Codeigniter 3.1.4 

# php -- BEGIN cPanel-generated handler, do not edit 
# Set the 「ea-php56」 package as the default 「PHP」 programming language. 
<IfModule mime_module> 
    AddType application/x-httpd-ea-php56 .php .php5 .phtml 
</IfModule> 
# php -- END cPanel-generated handler, do not edit 

# BEGIN cPanel-generated php ini directives, do not edit 
# Manual editing of this file may result in unexpected behavior. 
# To make changes to this file, use the cPanel MultiPHP INI Editor (Home >> Software >> MultiPHP INI Editor) 
# For more information, read our documentation (https://go.cpanel.net/EA4ModifyINI) 
<IfModule php5_module> 
    php_flag asp_tags On 
    php_flag display_errors On 
    php_value max_execution_time 30 
    php_value max_input_time 60 
    php_value max_input_vars 1000 
    php_value memory_limit 512M 
    php_value post_max_size 128M 
    php_value session.gc_maxlifetime 1440 
    php_value session.save_path "/var/cpanel/php/sessions/ea-php56" 
    php_value upload_max_filesize 128M 
    php_flag zlib.output_compression Off 
</IfModule> 
# END cPanel-generated php ini directives, do not edit 
<IfModule mod_rewrite.c> 
    RewriteEngine on 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^(.*)$ index.php/$1 [L,QSA] 
</IfModule>