How to redirect page with query-string to external webpage using APACHE RewriteRule

瘦欲@ 提交于 2019-12-12 18:25:09

问题


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

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

I have been testing with RewriteCond and RewriteRule without any luck.


回答1:


You want something like this?

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.

  1. Check if Special:UserLogin is in the request URI or the query string.
  2. Create backreference matches for the ? mark, the URI and the query string (this is very important)
  3. Redirect the request to https://login.security.server.com/test/UI/Login, but using the back references from the previous condition to build the goto= param, and using the B flag, which URL encodes the backreferences. This way, the result is an entire URL, along with query string, that's been URL encoded. (The NE flag is there to make sure the % signs themselves don't get double encoded).

With these rules, a request for:

/MyWiki/index.php?title=Special:UserLogin&returnto=Main_Page

Will get redirected to:

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

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

entirely intact.



来源:https://stackoverflow.com/questions/12202052/how-to-redirect-page-with-query-string-to-external-webpage-using-apache-rewriter

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