RewriteRule将/ a / b转换为?x = a& y = b?

时间:2015-05-14 08:45:49

标签: regex apache .htaccess mod-rewrite

这是我目前的重写规则,它允许我传递 domain.com/pagename 这样的网址,并将其评估为 domain.com/index.php?page=pagename < /强>:

<IfModule mod_rewrite.c>
RewriteEngine on
#Create friendly URL
RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /index.php?page=$1 [L,QSA]
</IfModule>

我需要做什么/添加以允许它采用这样的URL:

domain.com/pagename/type/value

并将其评估为:

domain.com/index.php?page=pagename&t=type&v=value

(其中/ type和/ value都是可选的)

...

谢谢!

1 个答案:

答案 0 :(得分:2)

你可以这样做:

<IfModule mod_rewrite.c>
RewriteEngine on

#Create friendly URL
RewriteRule ^index\.php$ - [L]

# If the request is for a valid file - OR
RewriteCond %{REQUEST_FILENAME} -f [OR]
# If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d
# then do nothing thus ignore all rules below this rule
RewriteRule ^ - [L]

# one parameter rule with numbers
RewriteRule ^(\d+)/?$ index.php?page=$1 [L,QSA]

# one parameter rule
RewriteRule ^([^/]+)/?$ index.php?page=$1 [L,QSA]

# two parameter rule
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?page=$1&t=$2 [L,QSA]

# three parameter rule
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?page=$1&t=$2&v=$3 [L,QSA]

</IfModule>