1

我的应用程序最多可以使用 6 个查询变量,以“干净”的 url 重写。所以我在 .htaccess 文件中设置了 URL 重写,哪种工作方式(一些边缘情况可能与此代码无关)但我想知道:是否有更有效的编写方式?

<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>
4

1 回答 1

1

一般来说,它看起来不错,但可以进行一些改进,例如

  1. 使用QSA(Query String 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]
于 2013-09-26T09:36:40.373 回答