Chuyển Apache .htaccess rewriterule sang nginx webserver

Thảo luận trong 'Nginx' bắt đầu bởi xman, 20/7/15.

  1. xman

    xman Administrator Thành viên BQT

    Tham gia ngày:
    6/7/15
    Bài viết:
    44
    Đã được thích:
    2
    Điểm thành tích:
    8
    Nginx không hỗ trợ file htaccess và rule rewrite cũng khác, nên nếu muốn rewrite url cho các code viết url riêng cho từng url thì các bạn có thể làm như ví dụ sau:

    Apache .htaccess

    Mã:
    RewriteRule ^trang-chu$ index.php?mod=page [L]
    
    
    
    RewriteRule ^ban-tin/(.*)-([0-9]+)/trang-([0-9]+).html$ index.php?mod=news&task=list_news&parent=$1&parent_id=$2&page=$3 [L]
    RewriteRule ^ban-tin/(.*)-([0-9]+)$ index.php?mod=news&task=list_news&parent=$1&parent_id=$2 [L]
    RewriteRule ^ban-tin/trang-([0-9]+).html$ index.php?mod=news&task=list_all&page=$1 [L]
    RewriteRule ^ban-tin$ index.php?mod=news&task=list_all [L]
    RewriteRule ^ban-tin/(.*)-([0-9]+).html$ index.php?mod=news&task=detail&link=$1&id=$2 [L]
    RewriteRule ^gioi-thieu$ index.php?mod=news&task=intro [L]
    
    
    RewriteRule ^thu-vien/(.*)-([0-9]+)/trang-([0-9]+).html$ index.php?mod=product&task=list_product&parent=$1&parent_id=$2&page=$3 [L]
    RewriteRule ^thu-vien/(.*)-([0-9]+)$ index.php?mod=product&task=list_product&parent=$1&parent_id=$2 [L]
    RewriteRule ^thu-vien/trang-([0-9]+).html$ index.php?mod=product&task=list_all&page=$1 [L]
    RewriteRule ^thu-vien$ index.php?mod=product&task=list_all [L]
    RewriteRule ^thu-vien/(.*)-([0-9]+).html$ index.php?mod=product&task=detail&link=$1&id=$2 [L]
    
    
    RewriteRule ^chuong-trinh-hoc/(.*)-([0-9]+)/trang-([0-9]+).html$ index.php?mod=educate&task=list_educate&parent=$1&parent_id=$2&page=$3 [L]
    RewriteRule ^chuong-trinh-hoc/(.*)-([0-9]+)$ index.php?mod=educate&task=list_educate&parent=$1&parent_id=$2 [L]
    RewriteRule ^chuong-trinh-hoc/trang-([0-9]+).html$ index.php?mod=educate&task=list_all&page=$1 [L]
    RewriteRule ^chuong-trinh-hoc$ index.php?mod=educate&task=list_all [L]
    RewriteRule ^chuong-trinh-hoc/(.*)-([0-9]+).html$ index.php?mod=educate&task=detail&link=$1&id=$2 [L]
    
    RewriteRule ^tin-tuc/(.*)-([0-9]+).html$ index.php?mod=news&task=detail&link=$1&id=$2 [L]
    RewriteRule ^lien-he$ index.php?mod=contact [L]
    
    RewriteRule ^doi-tac/(.*)-([0-9]+)$ index.php?mod=partner&task=list_partner&category=$1&product_category_id=$2 [L]
    RewriteRule ^doi-tac$ index.php?mod=partner&task=list_all [L]
    
    
    RewriteRule ^dich-vu-([0-9]+)/(.*)-([0-9]+)/(.*)-([0-9]+)$ index.php?mod=product&task=list_service2&parent_id=$1&category=$2&product_category_id=$3&sub=$4&product_sub_id=$5 [L]
    
    RewriteRule ^dich-vu-([0-9]+)/(.*)-([0-9]+)$ index.php?mod=product&task=list_service_category&parent_id=$1&category=$2&product_category_id=$3 [L]
    
    
    RewriteRule ^dich-vu-([0-9]+)$ index.php?mod=product&task=list_service&parent_id=$1 [L]
    RewriteRule ^(.*)-([0-9]+)/(.*)-([0-9]+)$ index.php?mod=product&task=list_product_category&parent=$1&parent_id=$2&category=$3&product_category_id=$4 [L]
    RewriteRule ^(.*)-([0-9]+)$ index.php?mod=product&task=list_product&parent=$1&parent_id=$2 [L]
    Chuyển sang Nginx:
    Mã:
    # BEGIN FIX FOR NGINX SERVER
    rewrite ^/trang-chu$ /index.php?mod=page last;
    
    rewrite ^/ban-tin$ /index.php?mod=news&task=list_all last;
    rewrite ^/ban-tin/trang-([0-9]+)\.html$ /index.php?mod=news&task=list_all&page=$1 last;
    rewrite ^/ban-tin/(.*)-([0-9]+)\.html$ /index.php?mod=news&task=detail&link=$1&id=$2 last;
    rewrite ^/ban-tin/(.*)-([0-9]+)$ /index.php?mod=news&task=list_news&parent=$1&parent_id=$2 last;      
    rewrite ^/ban-tin/(.*)-([0-9]+)/trang-([0-9]+)\.html$ /index.php?mod=news&task=list_news&parent=$1&parent_id=$2&page=$3 last;
    
    rewrite ^/gioi-thieu$ /index.php?mod=news&task=intro last;
    
    rewrite ^/thu-vien/(.*)-([0-9]+)/trang-([0-9]+).html$ /index.php?mod=product&task=list_product&parent=$1&parent_id=$2&page=$3 last;
    rewrite ^/thu-vien/(.*)-([0-9]+)$ /index.php?mod=product&task=list_product&parent=$1&parent_id=$2 last;
    rewrite ^/thu-vien/trang-([0-9]+).html$ /index.php?mod=product&task=list_all&page=$1 last;
    rewrite ^/thu-vien$ /index.php?mod=product&task=list_all last;
    rewrite ^/thu-vien/(.*)-([0-9]+).html$ /index.php?mod=product&task=detail&link=$1&id=$2 last;
    
    
    rewrite ^/chuong-trinh-hoc/(.*)-([0-9]+)/trang-([0-9]+).html$ /index.php?mod=educate&task=list_educate&parent=$1&parent_id=$2&page=$3 last;
    rewrite ^/chuong-trinh-hoc/(.*)-([0-9]+)$ /index.php?mod=educate&task=list_educate&parent=$1&parent_id=$2 last;
    rewrite ^/chuong-trinh-hoc/trang-([0-9]+).html$ /index.php?mod=educate&task=list_all&page=$1 last;
    rewrite ^/chuong-trinh-hoc$ /index.php?mod=educate&task=list_all last;
    rewrite ^/chuong-trinh-hoc/(.*)-([0-9]+).html$ /index.php?mod=educate&task=detail&link=$1&id=$2 last;
    
    rewrite ^/tin-tuc/(.*)-([0-9]+).html$ /index.php?mod=news&task=detail&link=$1&id=$2 last;
    rewrite ^/lien-he$ /index.php?mod=contact last;
    
    rewrite ^/doi-tac/(.*)-([0-9]+)$ /index.php?mod=partner&task=list_partner&category=$1&product_category_id=$2 last;
    rewrite ^/doi-tac$ /index.php?mod=partner&task=list_all last;
    
    
    rewrite ^/dich-vu-([0-9]+)/(.*)-([0-9]+)/(.*)-([0-9]+)$ /index.php?mod=product&task=list_service2&parent_id=$1&category=$2&product_category_id=$3&sub=$4&product_sub_id=$5 last;
    
    rewrite ^/dich-vu-([0-9]+)/(.*)-([0-9]+)$ /index.php?mod=product&task=list_service_category&parent_id=$1&category=$2&product_category_id=$3 last;
    
    
    rewrite ^/dich-vu-([0-9]+)$ /index.php?mod=product&task=list_service&parent_id=$1 last;
    rewrite ^/(.*)-([0-9]+)/(.*)-([0-9]+)$ /index.php?mod=product&task=list_product_category&parent=$1&parent_id=$2&category=$3&product_category_id=$4 last;
    rewrite ^/(.*)-([0-9]+)$ /index.php?mod=product&task=list_product&parent=$1&parent_id=$2 last;
    
    #END FIX NGINX SERVER
    Sau đó lưu vào file .conf tương ứng với từng website trong thư mục:
    Mã:
    /etc/nginx/conf.d/
    Ngoài ra các bạn có thể tham khảo theo :

     

Chia sẻ trang này