2013-09-26 3 views
1

내 응용 프로그램은 "깨끗한"URL로 다시 작성하기 위해 최대 6 개의 쿼리 변수를 사용할 수 있습니다. 그래서 내가 작동하도록 어떤 종류의 (이 코드와 관련이없는 몇 가지 가장자리 경우) .htaccess 파일에서 URL 재 작성을 설정했지만 궁금하네요 : 거기에 그것을 쓰는 더 효율적인 방법은 무엇입니까?내 mod_rewrite 규칙 개선

<IfModule mod_rewrite.c> 
RewriteEngine on 
Options +FollowSymLinks 

# if the following conditions are met, SKIP the rewriteRules. 
RewriteCond %{REQUEST_FILENAME} -f [OR] 
RewriteCond %{REQUEST_URI} ^/v2/(_admin/|_css/|_js/|phpmyadmin/|_img/) [NC] 
RewriteRule . - [L] 



# Externally redirect to add missing trailing slash 
RewriteRule ^(([a-z0-9._\-]+/)*[a-z0-9_\-]+)$ http://%{HTTP_HOST}/$1/?%{QUERY_STRING} [NC,R,L] 

# SIX PARAMS 
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ index.php?section=$1&item=$2&menu=$3&content=$4&id=$5&title=$6&%{QUERY_STRING} [NC,L] 

# FIVE PARAMS 
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ index.php?section=$1&item=$2&menu=$3&content=$4&id=$5&%{QUERY_STRING} [NC,L] 

# FOUR PARAMS 
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ index.php?section=$1&item=$2&menu=$3&content=$4&%{QUERY_STRING} [NC,L] 

# THREE PARAMS : projects/touch/texts/ 
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?section=$1&item=$2&menu=$3&%{QUERY_STRING} [NC,L] 

# TWO PARAMS: downloads 
RewriteRule ^downloads/([^/]+)/$ index.php?section=downloads&item=$1&%{QUERY_STRING} [NC,L] 

# TWO PARAMS: 
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?section=$1&item=$2&%{QUERY_STRING} [NC,L] 

# TAG URL : index.php?tag=url+encoded+keyword 
RewriteRule ^tag/([a-z0-9_\-]+)/$ index.php?tag=$1&%{QUERY_STRING} [NC,L] 

# ONE PARAM 
RewriteRule ^([a-z0-9_\-]+)/$ index.php?section=$1&%{QUERY_STRING} [NC,L] 
</IfModule> 

답변

1

일반적으로보기에는 좋지만 몇 가지 개선 사항이있을 수 있습니다.

  1. 사용 QSA (쿼리 문자열 추가]) 플래그와 슬래시 규칙을 후행
  2. 도 많이

을 단순화 할 수 정규식에서

  • 사용 \w 대신 [A-Za-z-9_] 모든 시간을 %{QUERY_STRING} 추가하지 수정 된 코드 :

    Options +FollowSymLinks -MultiViews 
    RewriteEngine on 
    
    # if the following conditions are met, SKIP the rewriteRules. 
    RewriteCond %{REQUEST_FILENAME} -f [OR] 
    RewriteCond %{REQUEST_URI} ^/v2/(_admin/|_css/|_js/|phpmyadmin/|_img/) [NC] 
    RewriteRule . - [L] 
    
    # Externally redirect to add missing trailing slash 
    RewriteRule [^/]$ %{REQUEST_URI}/ [R,L] 
    
    # SIX PARAMS 
    RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ index.php?section=$1&item=$2&menu=$3&content=$4&id=$5&title=$6&%{QUERY_STRING} [NC,L] 
    
    # FIVE PARAMS 
    RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ index.php?section=$1&item=$2&menu=$3&content=$4&id=$5&%{QUERY_STRING} [NC,L] 
    
    # FOUR PARAMS 
    RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ index.php?section=$1&item=$2&menu=$3&content=$4 [QSA,NC,L] 
    
    # THREE PARAMS : projects/touch/texts/ 
    RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?section=$1&item=$2&menu=$3 [QSA,NC,L] 
    
    # TWO PARAMS: downloads 
    RewriteRule ^downloads/([^/]+)/$ index.php?section=downloads&item=$1 [QSA,NC,L] 
    
    # TWO PARAMS: 
    RewriteRule ^([^/]+)/([^/]+)/?$ index.php?section=$1&item=$2 [QSA,NC,L] 
    
    # TAG URL : index.php?tag=url+encoded+keyword 
    RewriteRule ^tag/([\w-]+)/$ index.php?tag=$1 [NC,L,QSA] 
    
    # ONE PARAM 
    RewriteRule ^([\w-]+)/$ index.php?section=$1 [L,QSA]