如何动态编写.htaccess(即从查询字符串)

时间:2011-08-08 06:16:55

标签: .htaccess

Bellow是我的基本.htaccess代码

Options +FollowSymLinks
RewriteEngine on
RewriteBase /artist/
RewriteRule ^member/([^/]*)$ /artist/index.php?member=$1

Bellow是我的静态.htaccess代码

Options +FollowSymLinks
RewriteEngine on
RewriteBase /artist/
#we module doing some action
RewriteRule ^member/([^/]*)/([^/]*)/action/([^/]*)/$ /artist/index.php?member=$1&module=$2&action=$3
#action operation e.g create
RewriteRule ^member/([^/]*)/([^/]*)/action/([^/]*)/opt/([^/]*)/$ /artist/index.php?member=$1&module=$2&action=$3&opt=$4
#if only module calls
RewriteRule ^member/([^/]*)/([^/]*)/$ /artist/index.php?member=$1&module=$2
RewriteRule ^member/([^/]*)/([^/]*)/opt/([^/]*)/$ /artist/index.php?member=$1&module=$2&opt=$3
RewriteRule ^member/([^/]*)$ /artist/index.php?member=$1
RewriteRule ^member/0/$ /artist/index.php?member=$1
RewriteRule ^member/([^/]*)/([^/]*)/opt/([^/]*)/$ /artist/index.php?member=$1&module=$2&opt=$3  
RewriteRule ^member/([^/]*)/([^/]*)/opt/([^/]*)/topicId/([^/]*)/$ /artist/index.php?member=$1&module=$2&opt=$3&topicId=$4

任何人都可以帮我编写动态RewriteRule来在我的RewiteRule中添加查询sting,这样我就不必手动添加RewriteRule而且还会减少我的.htaccess代码行

有点像:

RewriteRule ^member/([^/]*)[dynamic code]$ /artist/index.php?member=$1[dynamic code here]

1 个答案:

答案 0 :(得分:1)

此时,您将所有请求重写为/artist/index.php而不构建查询字符串,index.php检查请求URI以填充其自己的变量。

即。只有一个规则

RewriteRule ^member/.* /artist/index.php

index.php可以在斜杠上查看$_SERVER['REQUEST_URI']explode等。

mod_rewrite的工作不是实现复杂的逻辑。你把它放在你的申请表中。

http://en.wikipedia.org/wiki/Front_Controller_pattern