简体   繁体   中英

.htaccess redirect query strings

This was kind of hard to search for, I found examples of other work but basically I have a url:

site.com/?page=test&id=3

I want to rewrite as: site.com/test:3

with 'test' being page, and 3 being id.

I have the following match for equal values:

RewriteCond %{QUERY_STRING} ^(\w+)=(\w+)$
RewriteRule ^/ /%1:%2?

But that only works for the key and the value, what regex can I use to select just the key, with %1 and %2 ?

Thanks,

My Second Attempt: (I only need on index.php)

RewriteCond %{QUERY_STRING} ^page=(\w+)&id=([0-9]*)$
RewriteRule ^index\.php$ /%1:%2**?** [R=302,L]

but now a 404 error is occuring when I do: index.php/details:1

You can use this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On

RewriteCond %{THE_REQUEST} \?test=([^\s&]+)&test2=([^\s&]+) [NC]
RewriteRule ^/?$ /%1:%2? [R=302,L,NE]

RewriteRule ^([^:]+):([^/]+)/?$ /?test=$1&test2=$2 [L,QSA]

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