简体   繁体   English

如何从路径到查询进行mod_rewrite?

[英]How to mod_rewrite from path to query?

I have url like: 我有这样的网址:

http://olddomain.com/demo?abc=ced&ghi=jkl

And I would like to rewrite it to separate domain as query: 我想将其重写为单独的域作为查询:

http://newdomain.com/legacy?url=http%3A%2F%2Folddomain.com%2Fdemo%3Fabc%3Ddef%26ghi%3Djkl

Right now I have something like this: 现在我有这样的事情:

RewriteRule (.*) http://newdomain.com/legacy?url=%{HTTP_HOST}%{REQUEST_URI}?%{QUERY_STRING}

The problem is that the output is: 问题是输出是:

http://newdomain.com/?url=olddomain.com/demo?abc=def&ghi=jkl

So no http:// and characters /, ?, = and & are not escaped. 因此,没有http://和字符/,?,=和&不能转义。 In addition to that if there will be no query in original requests there will be trailing ?, but that's probably not big problem. 除此之外,如果原始请求中不存在查询,则会出现尾随?,但这可能不是什么大问题。

You may try this: 您可以尝试以下方法:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/demo [NC]
RewriteRule .*   http://newdomain.com/legacy?url=http://%{HTTP_HOST}%{REQUEST_URI}  [L,R,QSA]

Redirects 重新导向

http://olddomain.com/demo?abc=ced

To

http://newdomain.com/legacy?url=http://olddomain.com/demo?abc=ced

All strings are assumed to be fixed, except the incoming query. 除传入查询外,所有字符串均假定为固定的。

As there was no response to my comment, this answer assumes: 由于没有回应我的评论,因此该答案假定:

  • &ghi=jkl in the query appended to the substitution URL (In the question), is also part of the query in the incoming URL 查询中的&ghi=jkl附加到替代URL(在问题中),也是传入URL中查询的一部分

  • http://example.com/ is the same http://newdomain.com/ . http://example.com/是相同的http://newdomain.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM