简体   繁体   English

htaccess 301使用查询字符串重定向

[英]htaccess 301 redirect with query string

I have this htaccess code that redirects a php file with url parameters to an html file. 我有此htaccess代码,可将带有url参数的php文件重定向到html文件。

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 目前,它会重定向sampledomain.com/test/view.php?file=1232&text=456

to sampledomain.com/test/123&text=456.html 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 我将需要重定向,仅将第一个参数值保留为html文件名,例如: sampledomain.com/test/123.html

Can someone please post the correct htaccess code? 有人可以张贴正确的htaccess代码吗?

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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