htaccess动态重写规则

时间:2013-12-05 15:55:26

标签: php regex apache .htaccess mod-rewrite

我有一些请求参数,我将其转换为友好的URL

例如:

http://domain.com/members/girls/sort/last_reg/nick/ted/from/18/to/28/hfrom/150/hto/190/bbody/Athletic/

参数是动态的,因此URL可以是

http://domain.com/members/girls/nick/ted/from/18/to/28/hfrom/150/hto/190/bbody/Athletic/

http://domain.com/members/men/nick/ted/to/28/hfrom/150/hto/190/bbody/Athletic/

参数是成对的

parameter_name/parameter_value

唯一不变的是

http://domain.com/members/

如何将此网址转换为

http://domain.com/filename.php?sex=men&pram1=value1&param2=value2 ......

http://domain.com/filename.php?sex=girls&pram1=value1&param2=value2 ......

2 个答案:

答案 0 :(得分:2)

将此代码放入DOCUMENT_ROOT/.htaccess文件中:

RewriteEngine On

## recucrsion based rule to convert 
## /members/men/n1/v1/n2/v2 to /members.php?n2=v2&n1=v1&sex=men

RewriteRule ^(members)/(girls|men)(/.*)?$ /$1/$3?sex=$2 [L,QSA,NC]

RewriteRule ^(members)/([^/]*)/([^/]*)(/.*)?$ /$1/$4?$2=$3 [L,QSA,NC]

RewriteRule ^(members)/?$ /$1.php [L,NC]

答案 1 :(得分:0)

这可能有所帮助:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)/(.*)/(.*)/$ filename.php?$1=$2&$3=$4 [L,QSA]