[英]Redirect specific Url variables
My site used to generate URLs like this: 我的网站曾经生成过这样的网址:
/data.php?s=1432862823&type=basic
I have modified the program so the new URLs generated are: 我已经修改了程序,所以生成的新URL是:
/d.php?s=1432862823&t=basic
Since some people have bookmarked the old URLs I want to write a Rewrite rule that will get them to the new url. 由于有些人已将旧的URL添加为书签,因此我想编写一个Rewrite规则,以将其链接到新的URL。
I've not this so far: 到目前为止,我还没有:
RewriteEngine on
RewriteRule "^/data\.php$" "/d.php"
but I can't figure out how to account for the variables. 但我不知道如何解释这些变量。
Try this : 尝试这个 :
RewriteEngine On
RewriteCond %{QUERY_STRING} ^s=([^&]+)&type=([^&]+) [NC]
RewriteRule ^data.php$ /d.php?s=%1&t=%2 [NC,R,L]
This will externally redirect a request for : 这将从外部重定向以下请求:
/data.php?s=foo&type=bar
to 至
/d.php?s=foo&t=bar
Edit: I do apologise, I only noticed now that you have changed the type
parameter to t
. 编辑:我很抱歉,我只注意到现在您已将type
参数更改为t
。 As such, this solution will not fit your needs, unless for a URI where the query string parameters do not change. 因此,除非查询字符串参数不变的URI,否则此解决方案将无法满足您的需求。 I'm leaving this answer here so that others may learn from it - you'll be surprised how many people don't know that the query string is automatically transferred to the new destination. 我在这里留下了这个答案,以便其他人可以从中学到—您会惊讶地发现,有很多人不知道查询字符串会自动转移到新的目的地。 Starkeen's answer, therefore, is the correct answer. 因此,Starkeen的答案是正确的答案。
You could follow Starkeen's solution, which specifically checks for the query string and explicitly adds it to d.php
. 您可以遵循Starkeen的解决方案,该解决方案专门检查查询字符串并将其显式添加到d.php
。 Alternatively, for simplicity, you could just use this: 另外,为简单起见,您可以使用以下代码:
RewriteEngine on
RewriteRule ^data.php$ /d.php [R=302,L]
The query string will automatically be transferred from data.php
to d.php
, and you will be redirected accordingly. 查询字符串将自动从data.php
传输到d.php
,并将相应地重定向您。
To make the redirect permanent and cached by browsers and search engines, change 302
to 301
. 要使重定向永久化并由浏览器和搜索引擎缓存,请将302
更改为301
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.