2013-09-26 2 views
1

Мое приложение может использовать до 6 переменных запроса, которые должны быть переписаны в «чистых» URL-адресах. Итак, я настраиваю переписывание URL-адресов в файле .htaccess, какие работы (несколько краевых случаев, вероятно, не связаны с этим кодом), но мне интересно: есть ли более эффективный способ его записи?Улучшение моих правил 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 (Строка запроса Append) флаг и избежать добавления %{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] 
Смежные вопросы