简体   繁体   中英

htaccess 301 redirect with query string

I have this htaccess code that redirects a php file with url parameters to an html file.

RewriteEngine on
RewriteCond %{QUERY_STRING} ^file=(.*)$
RewriteRule ^test/view\.php$ /test/%1.html? [R=301,L]

It currently redirects sampledomain.com/test/view.php?file=1232&text=456

to sampledomain.com/test/123&text=456.html

I would need to redirect keeping only the first parameter value as html file name like this sampledomain.com/test/123.html

Can someone please post the correct htaccess code?

Thanks

Try:

RewriteEngine On
RewriteCond %{THE_REQUEST} \?file=([^&\ ]+)&text=([&\ ]+)
RewriteRule ^test/view\.php$ /test/%1/%2.html? [L,R=301]

You need to capture only first query parameter instead of using .* which captures everything:

RewriteEngine on

RewriteCond %{QUERY_STRING} (?:^|&)file=([^&]+)
RewriteRule ^test/view\.php$ /test/%1.html? [R=301,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