开发者

URL rewriting, chain structured url for single point of entery

RewriteEngine on

Rewrit开发者_如何学运维eRule ^([a-zA-Z0-9-_]+)$ /index.php?mainp=$1&%{QUERY_STRING}
RewriteRule ^([a-zA-Z0-9-_]+)/$ /index.php?mainp=$1&%{QUERY_STRING}
RewriteRule ^([a-zA-Z0-9-_]+)/([a-zA-Z0-9-_]+)$ /index.php?mainp=$1&subp=$2&%{QUERY_STRING}
RewriteRule ^([a-zA-Z0-9-_]+)/([a-zA-Z0-9-_]+)/$ /index.php?mainp=$1&subp=$2&%{QUERY_STRING}
RewriteRule ^([a-zA-Z0-9-_]+)/([a-zA-Z0-9-_]+)/([a-zA-Z0-9-_]+)$ /index.php?mainp=$1&subp=$2&id=$3&%{QUERY_STRING}
...

I don't think this is the best way to write this rule. Any help would be greatly appreciated.


RewriteRule ^([a-z0-9-_]+)/?$ /index.php?mainp=$1 [QSA,NC,L]
RewriteRule ^([a-z0-9-_]+)/([a-z0-9-_]+)/?$ /index.php?mainp=$1&subp=$2 [QSA,NC,L]
RewriteRule ^([a-z0-9-_]+)/([a-z0-9-_]+)/([a-z0-9-_]+)/?$ /index.php?mainp=$1&subp=$2&id=$3 [QSA,NC,L]
  1. By adding ? before $ you making trailing slash optional, so 1 of such rules will cover 2 of yours (line 1 = 1 & 2; line 2 = 3 & 4, line 3 = 5 & 6 (6 is not listed but I guess it does exist))

  2. I have replaced &%{QUERY_STRING} by [QSA] flag -- does exactly the same (can even be considered more convenient).

  3. I have also removed A-Z from patterns and replaced by [NC] flag (NC = ignore case). This makes all rules a bit shorter and therefore a bit easier to read.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜