Thứ Hai, 2 tháng 4, 2012

htaccess cho Codeigniter tối ưu URL


Hướng dẫn tối ưu hóa URL cho website viết bắng Codeigniter bắng file .htaccess

URL của CI thông thường theo cấu trúc như sau: example.com/index.php/products/view/shoes.html
Thêm vào file .htaccess đưa URL về dạng: example.com/products/view/shoes.html bỏ index.php

File .htaccess được đặt trong thư mục gốc của site và có nội dung như sau:

<IfModule mod_rewrite.c>

    Options +FollowSymLinks
    RewriteEngine on

    # Keep people out of codeigniter directory and Git/Mercurial data
    RedirectMatch 403 ^/(application\/cache|codeigniter|\.git|\.hg).*$

    # Send request via index.php (again, not if its a real file or folder)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    <IfModule mod_php5.c>
        RewriteRule ^(.*)$ index.php/$1 [L]
    </IfModule>

    <IfModule !mod_php5.c>
        RewriteRule ^(.*)$ index.php?/$1 [L]
    </IfModule>

</IfModule>


Ngoài ra file .htaccess ta còn có thể viết thêm các phần chuyển hướng cho các lỗi như 301, 402, 404,...

URL của chúng ta có cấu trúc như sau:

domain/thư mục/controller/action/id

Muốn tối ưu hóa tốt hơn nữa ta có thể khai báo thêm về phần cấu trúc URL trong application/config/routes.php để có thể có được URL như: example.com/san-pham/1223-dien-thoai-di-dong.html để có thể tối ưu thêm cho SEO

Tham khảo Thêm:

http://codeigniter.com/user_guide/general/urls.html

http://codeigniter.com/user_guide/general/routing.html

Không có nhận xét nào:

Đăng nhận xét