简体   繁体   中英

mod_rewrite GET form to clean URL

I have tried searching but only came up with this link which I can get to work, just not correctly to apply to my current situation.

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]

As of right now, it will redirect

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

to

http://localhost/highscores/personal/test

like it should.

But I have a compare function which submits a GET request like:

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

which needs to come out like

http://localhost/highscores/personal/test/vs/test2

but it comes out like

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

Edit: Resolved using help from 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]

You need to edit your rule

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

to

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

because of greedy regex. You need to break (.*) section if / is found

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM