mod_rewrite GET表单来清理URL

时间:2015-03-30 00:00:22

标签: php forms apache .htaccess mod-rewrite

我尝试过搜索,但只提出this link我可以开始工作,只是不正确地适用于我目前的情况。

RewriteCond %{REQUEST_URI} ^/highscores/personal/$
RewriteCond %{QUERY_STRING} ^user1=(.*)$
RewriteRule ^(.*)$ /highscores/personal/%1? [R=301,L,QSA]

RewriteRule ^highscores/personal/(.*)/vs/(.*) personal.php?user1=$1&user2=$2 [L,QSA]
RewriteRule ^highscores/personal/(.*) personal.php?user1=$1 [L,QSA]
RewriteRule ^highscores/skill/(.*) highscores.php?skill=$1 [L,QSA]
RewriteRule ^highscores/(.*) highscores.php?$1 [L,QSA]

截至目前,它将重定向

http://localhost/highscores/personal/?user1=test

http://localhost/highscores/personal/test

喜欢它。

但我有一个比较函数,它提交一个GET请求,如:

http://localhost/highscores/personal/?user1=test&user2=test2

需要像

一样出现
http://localhost/highscores/personal/test/vs/test2

但它就像

一样
http://localhost/highscores/personal/test&user2=test2

修改:使用this htaccess tester

的帮助解决
RewriteCond %{REQUEST_URI} ^/highscores/personal/$
RewriteCond %{QUERY_STRING} ^user1=([^&]+)&user2=(.*)$
RewriteRule ^(.*)$ /highscores/personal/%1/vs/%2? [R=301,L]
RewriteCond %{REQUEST_URI} ^/highscores/personal/$
RewriteCond %{QUERY_STRING} ^user1=(.*)$
RewriteRule ^(.*)$ /highscores/personal/%1 [R=301,L]

1 个答案:

答案 0 :(得分:1)

您需要修改规则

RewriteRule ^highscores/personal/(.*)/vs/(.*) personal.php?user1=$1&user2=$2 [L,QSA]

RewriteRule ^highscores/personal/([^/]+)/vs/(.*) personal.php?user1=$1&user2=$2 [L,QSA]

因为贪婪的正则表达式。如果找到(.*),则需要中断/部分