开发者

.htaccess, two consecutive rewrites?

I need to take a url, "/ServiceSearch/r.php?n=blahblah", and have it go to "/search/blahblah/" so that it appears in the browser as "/search/blahblah", but I actually want it to REALLY be going to "r.php?n=ServiceSearch&n=blahblah"..

So I was thinking I'll need to rewrite the first URL to "/ServiceSea开发者_JAVA技巧rch/r.php?n=blahblah" and then the second url, "/search/blahblah/", to the third, "r.php?n=ServiceSearch&n=blahblah".

Well, I know this is wrong, but it's my best guess. I'm really struggling with it.


Well, I know this is wrong

No, that’s actually the right way. Something like the following should work:

RewriteRule /ServiceSearch/r.php?n=(.*)$ /search/$1 [R]
RewriteRule /search/(.*)$ /r.php?n=ServiceSearch&n=$1 [L]

Here, (.*) captures the variable part (“blablabla”) and inserts it into the replacement via $1. The flags at the end mean that the first query should be a HTTP redirect ([R]), i.e. the client’s browser will be instructed to redirect to that address. And that the second redirect is to be the last ([L] – it’s also not an HTTP redirect since we didn’t specify that; instead, the redirect is handled on the server side). Strictly speaking, the [L] flag isn’t necessary but if you later add more rewrite rules it will prevent unwanted interference.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜