简体   繁体   中英

Matching query string vars with mod_rewrite

I have URLs with the following query parameters:

/list.php?id=1&name=some-stuff.html
/list.php?id=18&name=some-another-stuff.html

How can I get them to look like this

/1-some-stuff.html 

or even

/some-stuff.html

I tried the following lines but I get 404 error:

RewriteCond %{QUERY_STRING} id=(\d+)&name=(\w+)
RewriteRule ^list.php /%1-%2? [L,R=301]

You need to rewrite the new url back to the list.php using an additional rule

RewriteCond %{QUERY_STRING} id=(\d+)&name=(\w+)
RewriteRule ^list.php /%1-%2? [L,R=301]
RewriteRule ^([^-]+)-([^/]+)/?$ /list.php?id=$1&name=$2 [END]

EDIT : END flag does'nt work on all versions of apache ,if you are using apache version bellow 2.4, you can use the the following with %{THE_REQUEST}

RewriteCond %{THE_REQUEST} /list\.php\?id=(\d+)&name=([^\s&]+) [NC]
RewriteRule ^ /%1-%2? [L,R=301]
RewriteRule ^([^-]+)-([^/]+)/?$ /list.php?id=$1&name=$2 [L]

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