簡體   English   中英

Codeigniter的URL重寫

[英]Codeigniter's URL Rewriting

我正在使用以下htaccess腳本,以便我可以從URI隱藏index.php

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

但我面臨一個重大問題:(

我的index.php文件旁邊有一個名為assets的目錄,它應該在那里。 當我通過瀏覽器瀏覽目錄時,將顯示Codeigniter 未找到的頁面。 我無法瀏覽文件/assets/image.jpg但是當我從<img>標簽調用它時它會顯示

我現在能做什么?

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

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,此版本稍作修改。

這是我用於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> 

這是我正在使用的完整解決方案,並且發現效果很好:

將此代碼段放在項目根目錄下的.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]

並在位於system \\ application \\ config \\ config.php的CI配置文件中

$config['uri_protocol'] = "REQUEST_URI";
# 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]

我使用它,它完美地工作!

將以下內容插入.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]

暫無
暫無

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

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