Trying to make a GET variable invisible in an URL but retain its usefulness using mod_write

故事扮演 提交于 2019-12-17 20:29:36

问题


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]

回答1:


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).




回答2:


If you are not passing the "ID" as part of the query string (e.g. ?id=5) or part of the URI (e.g. /preview/about/5) then you need to pass it in the request body, in something like a POST request. Otherwise, you can't make it "invisible", because the webserver isn't going to see it. If the webserver doesn't see it as a request, there is nothing mod_rewrite can possibly do to extract it.

Assuming you can't setup your site so that requests get POSTed (sort of like how a form is submitted) everytime someone clicks on a link, you're best bet is probably having it look like the http://www.domainname.com/preview/about/5 form, or maybe http://www.domainname.com/preview/about-5?



来源:https://stackoverflow.com/questions/17830707/trying-to-make-a-get-variable-invisible-in-an-url-but-retain-its-usefulness-usin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!