Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
419 views
in Technique[技术] by (71.8m points)

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

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]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...