试图让一个GET变量无形的URL,但使用mod_write保留它的用处 [英] Trying to make a GET variable invisible in an URL but retain its usefulness using mod_write

查看:248
本文介绍了试图让一个GET变量无形的URL,但使用mod_write保留它的用处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好所有,

我想掌握,mod_rewrite的魔法和需要一些意见/帮助。

我想打开一个URL地址:

  http://www.domainname.com/$p$pview/about/5
 

要这样:

  http://www.domainname.com/$p$pview/about
 

问题是,我仍然需要保留[ID]的原URL的一部分被用来作为一个GET以后,它是不可见的。

在code我迄今:

 重写规则^ preVIEW \ /([^ /] +)\ /([^ /] +)\ / $ / preVIEW \ / $ 1&ID = $ 2 [R = 301,QSA]
重写规则^ preVIEW \ /([^ /] +)\ / $模式= preVIEW和放大器;?ID = $ 2 [L,QSA]
 

本管理,以创建像一个网址: http://www.domainname.com/$p$pview/about/?id=5 并通过 ID 过,我只是需要?ID = 5是不可见的URL。

感谢你在前进的人谁拥有了一个解决方案,很多AP preciated。

更新:

我设法得到以下code按预期唉,这是使用静态值ID的所有我现在需要为这是完全是搞不定关闭动态值ID。

 重写规则^ preVIEW \ /([^ /] +)\ /([^ /] +)\ / $ / preVIEW \ / $ 1 [R = 301,QSA]
的RewriteCond%{QUERY_STRING}!* n = 5。* $
重写规则^ preVIEW \ /([^ /] +)\ / $模式= preVIEW和放大器;?ID = 5 [L,QSA]
 

解决方案

您不能得到'隐形'得到的参数。你会得到最接近的是设置一个cookie来传递这个数据以后。

 重写规则^ preVIEW /([^ /] +)/([^ /] +)[/] $ preVIEW / $ 1 / [CO = ID :$ 2:127.0.0.1:1:/ preVIEW / $ 1:0:1,R]
 

在PHP中,你可以访问这个cookie与 $ _ COOKIE ['身份证'] 和ID是不可见的URL(因为它不是实际存在)。有关 CO 标记文档可以发现的这里


编辑:如果你想要做的这一切mod_rewrite的,您可以访问mod_rewrite的这个cookie了。由于这是一个内部重写,你也许可以只使用一个直接的路径,你要拨打的实际文件。

 的RewriteCond%{} HTTP_COOKIE ID =([^;] *)
?重写规则^ preVIEW /([^ /] +)[/] $ preVIEW / $ 1 ID =%1 [CO = ID: - :127.0.0.1:-1:/$p$pview/ $ 1:0:1,结束]
 

EDIT2:我在为ID-的cookie中的第二条规则(到期时间T-1分钟)复位补充​​说。这将导致正确的页面加载,如果用户决定去 preVIEW /约/ 内将 $第1页分钟后重新$ PVIEW /约/ 5 (其重定向到 preVIEW /约/ 有一个隐藏的ID设置为5加载不同的东西)。

Good day all,

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

I am trying to turn an URL from:

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

To this:

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

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.

The code I have thus far:

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

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.

UPDATE:

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]

解决方案

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]

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.


Edit: 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: 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天全站免登陆