2010-06-08 54 views
6

我正在使用以下htaccess腳本,以便我可以從URI中隱藏index.phpCodeigniter的URL重寫

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

但我面臨的主要問題:(

我有一個名爲assetsindex.php文件旁邊的目錄,它應該在那裏。當我通過瀏覽器瀏覽的目錄,然後笨的未找到頁面顯示出來。我不能瀏覽文件/assets/image.jpg但它顯示,當我把它從<img>標籤

現在我該怎麼辦?

請注意,它在我的本地服務器(localhost)中工作,但不在活動服務器中。

回答

6

你們是這麼想的。

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} -s [OR] 
RewriteCond %{REQUEST_FILENAME} -l [OR] 
RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule ^.*$ - [NC,L] 
RewriteRule ^(.*)$ /index.php/$1 [NC,L] 

這將通過任何請求,Apache將不會發送給CodeIgniter。這樣你就可以自由地創建目錄而不需要更新你的重寫規則,並且你不需要在Apache中設置404處理(甚至在你的鏡像/資源目錄中)。

值得一提的是,Zend Framework的作者在他們的用戶指南中推薦了這一點。 CodeIgniter稍微修改了此版本。

0

這是我用我的CI .htaccess文件安裝:

如果在子文件夾中有它,你將需要編輯RewriteBase。

<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

你的意思是你還沒有移動的公共目錄之外的系統文件夾?當您(或您的管理員)在意外中禁用AllowOverride時會發生什麼? CI在他們的所有文件中都會亂糟糟地放棄,但是您是否已驗證過您的文件可以安全地作爲用戶的入口點?只需將系統文件夾移動到父文件夾並擁有單獨的公用目錄即可。 – coreyward 2010-06-18 03:44:11

+0

是的,這是真的,我移動我的生產服務器上的系統文件夾。 – 2010-06-20 23:08:47

0

這裏是我使用並找到很好的工作完整的解決方案:在你的項目根

將下面這段代碼片段中的.htaccess文件一樣http://localhost/myProject

RewriteEngine On 
RewriteBase /appDirName 
RewriteCond %{REQUEST_URI} ^appDirName.* 
RewriteRule ^(.*)$ /index.php/$1 [L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php/$1 [L] 

而在你的CI配置文件位於系統\應用程序\ config \ config.php

$config['uri_protocol'] = "REQUEST_URI"; 
0
# Turn on URL rewriting 
RewriteEngine On 
# Put your installation directory here: 
RewriteBase/
# Allow these directories and files to be displayed directly: 
# - index.php (DO NOT FORGET THIS!) 
# - robots.txt 
# - favicon.ico 
# - Any file inside of the images/, js/, or css/ directories 
RewriteCond $1 ^(index\.php|robots\.txt|favicon\.ico|images|js|css) 
# No rewriting 
RewriteRule ^(.*)$ - [PT,L] 
# Rewrite all other URLs to index.php/URL 
RewriteRule ^(.*)$ index.php/$1 [PT,L] 

我用這個,它工作完美!

0

將以下到您的.htaccess

# Change "welcome" to your default controller: 
    RewriteRule ^(welcome(/index)?|index(\.php)?)/?$/[L,R=301] 
    RewriteRule ^(.*)/index/$ $1 [L,R=301] 

# Checks to see if the user is attempting to access a valid file, 
# if not send them to index.php 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^(.*)$ index.php/$1 [L]