2010-06-08 7 views
6

Tôi đang sử dụng đoạn mã htaccess sau để tôi có thể ẩn index.php từ URI.Viết lại URL của Codeigniter

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

Nhưng tôi phải đối mặt với một vấn đề lớn :(

Tôi có một thư mục có tên assets bên cạnh tập tin index.php của tôi và nó nên có mặt ở đó. Khi tôi duyệt thư mục theo trình duyệt, sau đó CodeIgniter của không tìm thấy trang màn hình. tôi không thể duyệt các tập tin /assets/image.jpg nhưng nó sẽ hiển thị khi tôi gọi nó từ một thẻ <img>

tôi có thể làm gì bây giờ?

Lưu ý rằng nó đang hoạt động trên máy chủ cục bộ của tôi (localhost) nhưng không hoạt động trong máy chủ trực tiếp.

Trả lời

6

Các bạn đang nghĩ cách này.

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] 

Đó sẽ vượt qua bất kỳ yêu cầu rằng Apache sẽ không phục vụ tắt để CodeIgniter. Bằng cách này, bạn có thể tự do tạo các thư mục mà không cần cập nhật các quy tắc viết lại và bạn không cần thiết lập xử lý 404 trong Apache (ngay cả bên trong thư mục ảnh/tài nguyên của bạn).

Tín dụng đi tới các tác giả của khung công tác Zend đề xuất bit này trong hướng dẫn sử dụng của họ. Phiên bản này hơi được sửa đổi cho CodeIgniter.

0

Đây là tập tin .htaccess tôi sử dụng cho CI của tôi cài đặt:

Nếu bạn có nó trong một thư mục con bạn sẽ cần phải chỉnh sửa 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

Bạn có nghĩa là bạn chưa chuyển vào thư mục hệ thống bên ngoài của thư mục nào? Điều gì sẽ xảy ra khi bạn (hoặc quản trị viên của bạn) vô hiệu hóa AllowOverride? CI có hủy bỏ lộn xộn trên tất cả các tệp của họ, nhưng bạn đã xác minh rằng các tệp của mình có an toàn làm điểm truy cập cho người dùng không? Chỉ cần di chuyển thư mục hệ thống đến thư mục gốc và có một thư mục công cộng riêng biệt. – coreyward

+0

Có điều đó đúng và tôi đã di chuyển thư mục hệ thống của mình trên máy chủ sản xuất của mình. –

0

Đây là giải pháp hoàn chỉnh mà tôi đang sử dụng và thấy làm việc độc đáo:

Đặt đoạn mã này dưới đây trong tập tin .htaccess tại thư mục gốc dự án của bạn như 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] 

Và tại CI của bạn tệp cấu hình có tại system \ application \ 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] 

Tôi sử dụng tính năng này và nó hoạt động hoàn hảo!

0

Chèn đoạn mã sau vào .htaccess của bạn

# 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]