1

Ok, currently to get the URL in the format I want I use this in my .htaccess

RewriteRule    ^user.php/([0-9]+)/?$          user.php?id=$1    [NC,L]   
RewriteRule    ^user.php/edit/([0-9]+)/?$     user.php?id=$1&type=edit    [NC,L]   

Urls, redirect to

domain/user/7/      -> domain/user?id=7
domain/user/edit/7/ -> domain/user?id=7&type=edit

Works fine, but I want to be able to do better value pair urls instead of just strict matches.

An example is

domain/user/key/value/key/value/key/value/
    -> domain/user.php?key=value&key=value&key=value

I was looking at

.htaccess rewrite to convert directories into /key/value/key/value

The example works well if you just want to redirect from the main path, ie localhost/index.php, but I want to only be triggered when it's domain/user and I couldn't get it to match with using 'user' as the trigger

My current .htaccess

# Pretty url's
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

RewriteRule    ^user.php/([0-9]+)/?$          user.php?id=$1    [NC,L]   
RewriteRule    ^user.php/edit/([0-9]+)/?$     user.php?id=$1&type=edit    [NC,L]   

Is this possible, and if so how?

4

2 回答 2

1

Deja vu time!!!我在某个时候写了那个答案:P

以下是您可以为您的案例做的事情:

RewriteEngine On

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

RewriteRule    ^user.php/([0-9]+)/?$          user.php?id=$1    [NC,L]   
RewriteRule    ^user.php/edit/([0-9]+)/?$     user.php?id=$1&type=edit    [NC,L]   

# Pretty url's
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

使用这些规则,一个 URL/user/n1/v1/n2/v2/n3/v3/n4/v4将被 INTERNALLY 转发到/user.php?n4=v4&n3=v3&n2=v2&n1=v1 treating each pair of URL segments separated by / as a name-value pair forQUERY_STRING`。

于 2013-10-18T10:09:54.737 回答
1
Ok this is working for me

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

# Pretty url's
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

网址

/user/n1/v1/n2/v2/n3/v3/n4/v4

输出

Array ( [n4] => v4 [n3] => v3 [n2] => v2 [n1] => v1 ) 

阿努巴瓦,你这个男人:)

于 2013-10-18T11:27:14.623 回答