如何使用APACHE RewriteRule将带有查询字符串的页面重定向到外部网页 [英] How to redirect page with query-string to external webpage using APACHE RewriteRule
问题描述
我正在尝试将登录页面重定向到外部安全服务.验证凭据后,此服务将使用引荐来源网址将用户返回到原始页面,如以下示例所示:
I am trying to redirect a login page to an external security service. This service, after validating the credentials, will then return user back to the originating page using the referrer url, as in the following example:
http://{IP NUMBER}/MyWiki/index.php?title=Special:UserLogin&returnto=Main_Page
或对在query_string中包含 Special:UserLogin 的网站页面的任何调用都必须重定向到:
or any call to a page in the site containing Special:UserLogin in the query_string needs to be redirected to:
https://login.security.server.com/test/UI/Login?service=DSSEC&goto=http://{IP NUMBER}/MyWiki/index.php/Special:UserLogin
我一直在用RewriteCond和RewriteRule进行测试,没有任何运气.
I have been testing with RewriteCond and RewriteRule without any luck.
推荐答案
您想要这样的东西吗?
RewriteEngine On
RewriteCond %{REQUEST_URI} Special:UserLogin [OR]
RewriteCond %{QUERY_STRING} Special:UserLogin
RewriteCond ?#%{QUERY_STRING} ([^#]+)#([^#]+)
RewriteRule ^ https://login.security.server.com/test/UI/Login?service=DSSEC&goto=http://%{SERVER_ADDR}%{REQUEST_URI}%1%2 [L,B,NE]
好吧,这似乎有点令人困惑,但这是正在发生的事情.
Ok, this is going to seem a little confusing, but here's what's going on.
- 检查
Special:UserLogin
是否在请求URI或查询字符串中. - 为
?
标记,URI和查询字符串创建后向引用匹配项(这很重要) - 将请求重定向到
https://login.security.server.com/test/UI/Login
,但使用上一个条件中的后向引用来构建goto=
参数,并使用B
标志,该URL会对后向引用进行编码.这样,结果就是经过URL编码的整个URL以及查询字符串. (NE
标志用于确保%
标志本身不会被双重编码.)
- Check if
Special:UserLogin
is in the request URI or the query string. - Create backreference matches for the
?
mark, the URI and the query string (this is very important) - Redirect the request to
https://login.security.server.com/test/UI/Login
, but using the back references from the previous condition to build thegoto=
param, and using theB
flag, which URL encodes the backreferences. This way, the result is an entire URL, along with query string, that's been URL encoded. (TheNE
flag is there to make sure the%
signs themselves don't get double encoded).
使用这些规则,要求:
/MyWiki/index.php?title=Special:UserLogin&returnto=Main_Page
将重定向到:
https://login.security.server.com/test/UI/Login?service=DSSEC&goto=http://123.45.67.89/MyWiki/index.php%3ftitle%3dSpecial%3aUserLogin%26returnto%3dMain_Page
如您所见,查询字符串?title=Special:UserLogin&returnto=Main_Page
被编码为%3ftitle%3dSpecial%3aUserLogin%26returnto%3dMain_Page
,因此 login.security.server.com 不会将其误认为自己的查询字符串.相反,他们的登录服务将goto参数视为:
As you can see, the query string ?title=Special:UserLogin&returnto=Main_Page
gets encoded into %3ftitle%3dSpecial%3aUserLogin%26returnto%3dMain_Page
, so that the login.security.server.com doesn't mistake it for its own query string. Instead, their login service will see the goto parameter as:
http://123.45.67.89/MyWiki/index.php?title=Special:UserLogin&returnto=Main_Page
完好无损.
这篇关于如何使用APACHE RewriteRule将带有查询字符串的页面重定向到外部网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!