2013-09-26 68 views
1

我的應用程序可以使用最多6個查詢變量,在「乾淨」的URL中重寫。 所以我在.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(查詢字符串附加)標誌,並避免增加%{QUERY_STRING}所有的時間
  2. 使用\w而不是[A-Za-z-9_]在正則表達式
  3. 尾隨斜線規則還可以簡化很多

修改後的代碼:

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]