尝试使 GET 变量在 URL 中不可见,但使用 mod_write 保留其有用性 [英] Trying to make a GET variable invisible in an URL but retain its usefulness using mod_write

查看:14
本文介绍了尝试使 GET 变量在 URL 中不可见,但使用 mod_write 保留其有用性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在努力掌握 mod_rewrite 的魔法,需要一些建议/帮助.

I am trying to master the ,magic of mod_rewrite and require some advice/help.

我正在尝试从以下位置转换 URL:

I am trying to turn an URL from:

http://www.domainname.com/preview/about/5

为此:

http://www.domainname.com/preview/about

问题是,我仍然需要保留原始 URL 的 [id] 部分,以便稍后用作 GET 并且不可见.

The issue is, I still need to retain the [id] part of the original URL to be used as a GET later on and it not be visible.

我目前的代码:

RewriteRule ^preview/([^/]+)/([^/]+)/$         /preview/$1?id=$2 [R=301,QSA]
RewriteRule ^preview/([^/]+)/$                  ?mode=preview&id=$2 [L,QSA]

这设法创建一个 URL,如:http://www.domainname.com/preview/about/?id=5 并通过 ID 传递,我只需要 ?id=5 在 URL 中不可见.

This manages to create an URL like: http://www.domainname.com/preview/about/?id=5 and passes the ID through, I just need the ?id=5 to be invisible in the URL.

预先感谢任何对此有解决方案的人,非常感谢.

Thank you in advance anyone who has a solution for this, much appreciated.

更新:

我已经设法让下面的代码按预期工作,因为这是使用静态 ID 值,我现在需要完成的是让它处理 ID 的动态值.

I have managed to get the following code to work as expected alas this is using static values for ID all I now need for this to be complete is to get it working off dynamic values for ID.

RewriteRule ^preview/([^/]+)/([^/]+)/$       /preview/$1      [R=301,QSA]
RewriteCond                                     %{QUERY_STRING} !.*id=5.*$
RewriteRule ^preview/([^/]+)/$                ?mode=preview&id=5 [L,QSA]

推荐答案

您无法获取 'invisible' 获取参数.最接近的是设置一个 cookie 来传递这些数据.

You cannot get 'invisible' get parameters. The closest you'll get is setting a cookie to pass this data onwards.

RewriteRule ^preview/([^/]+)/([^/]+)[/]?$ preview/$1/ [CO=id:$2:127.0.0.1:1:/preview/$1:0:1,R]

在 php 中,您可以使用 $_COOKIE['id'] 访问此 cookie,并且 id 在 url 中是不可见的(因为它实际上并不存在).可以在此处.

In php you can access this cookie with $_COOKIE['id'] and the id is invisible in the url (because it isn't actually there). Documentation about the CO flag can be found here.

如果你想用 mod_rewrite 做这一切,你也可以从 mod_rewrite 访问这个 cookie.由于这是内部重写,因此您可能只需使用指向要调用的实际文件的直接路径即可.

If you want to do it all with mod_rewrite, you can access this cookie from mod_rewrite too. As this is an internal rewrite, you can probably just use a direct path to the actual file you want to call.

RewriteCond %{HTTP_COOKIE} id=([^;]*)
RewriteRule ^preview/([^/]+)[/]?$ preview/$1?id=%1 [CO=id:-:127.0.0.1:-1:/preview/$1:0:1,END]

Edit2:我在第二条规则中添加了 id-cookie 的重置(到期时间 T-1 分钟).如果用户决定在转到 preview/about/5 后 1 分钟内再次转到 preview/about/(重定向到 preview/about/code>preview/about/ 将隐藏 id 设置为5"以加载不同的内容).

I've added in a reset for the id-cookie in the second rule (expiry time T-1 minutes). This will cause the correct page to load if the user decides to go to preview/about/ again within 1 minute from going to preview/about/5 (which redirects to preview/about/ with a hidden id set to '5' to load something different).

这篇关于尝试使 GET 变量在 URL 中不可见,但使用 mod_write 保留其有用性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆