歡迎,
很長一段時間以來,我一直在努力解決將 url 重定向到單獨的 url 的問題,以防給出特殊的 baerer 令牌,我不想更改任何引數,只是進行匹配并將這個匹配發送到另一個 url。我的設定是來自郵遞員的 apache 2.4.41 典型請求:
https://xxxdddyyy.dqco1.firma-online.com/rest/auto-com/complete_addr?in_country=FR&in_line=73 avenue des lilas&access_token=988738467
重定向應該是這樣的:
http://tomcat-local:10022/rest/auto-com/complete_addr?in_country=FR&in_line=73 avenue des lilas&access_token=988738467
到目前為止我在 apache2.conf 中嘗試過的內容
RewriteCond %{QUERY_STRING} ^access_token=(988738467)$
RewriteRule (.*) http://tomcat-local:10022/ [R=301,L]
目標是具有特定令牌的客戶端將由特定的 tomcat 服務器提供服務
請幫助我,也許我做錯了什么,我也嘗試過 RedirectMatch,不幸的是沒有成功,我在https://regex101.com/上測驗的所有正則運算式
編輯:
仍然沒有,我不明白出了什么問題。我的配置
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)access_token=988738467($|&)
RewriteRule ^(.*)$ http://tomcat-local:10022/$1 [QSA,P,L]
</IfModule>
# disable some HTTP request types for security reasons
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_METHOD} ^TRACE
RewriteRule .* - [F]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_METHOD} !^(GET|POST|HEAD|OPTIONS)$
RewriteRule .* - [F]
</IfModule>
訪問日志:
"GET /rest/auto-com/complete_addr?in_country=FR&in_line=73 avenue des lilas&access_token=988738467 HTTP/1.1" "complete_addr" 200 200 1225 "-" "PostmanRuntime/7.28.4"
解決了
RewriteEngine On
RewriteOptions InheritDown
RewriteCond %{QUERY_STRING} (^|&)access_token=988738467($|&)
RewriteRule ^(.*)$ http://tomcat-local:10022/$1 [QSA,P,L]
# disable some HTTP request types for security reasons
RewriteCond %{REQUEST_METHOD} ^TRACE
RewriteRule .* - [F]
RewriteCond %{REQUEST_METHOD} !^(GET|POST|HEAD|OPTIONS)$
RewriteRule .* - [F]
問候保羅
uj5u.com熱心網友回復:
RewriteOptions InheritDown最后通過在重寫規則之前添加并添加RewriteEngine On到所有虛擬主機來解決此問題。
嘗試:
RewriteCond %{QUERY_STRING} (^|&)access_token=988738467($|&)
RewriteRule ^(.*)$ http://tomcat-local:10022/$1 [QSA,R=301,L]
這將重定向:
https://xxxdddyyy.dqco1.firma-online.com/rest/auto-com/complete_addr?in_country=FR&in_line=73 avenue des lilas&access_token=988738467http://tomcat-local:10022/rest/auto-com/complete_addr?in_country=FR&in_line=73 avenue des lilas&access_token=988738467如果access_token是_988738467https://xxxdddyyy.dqco1.firma-online.com/rest/auto-com/complete_addr?abc=123&access_token=988738467到http://tomcat-local:10022/rest/auto-com/complete_addr?abc=123&access_token=988738467
雖然我給了你這個解決方案,但我仍然對你為什么要外部重定向到本地頁面感到非常困惑。我相信你想要一個代理通行證。
解釋為什么這個Rewrite*問題不起作用:
^access_tokenin^access_token=(988738467)$表示它access_token必須是查詢字串中的第一個。(988738467)$表示988738467必須在查詢字串的末尾。
對于上述問題,僅https://https://xxxdddyyy.dqco1.firma-online.com/anything?access_token=988738467適用于您的RewriteCond.
編輯:
對于內部重定向到后端服務器:
RewriteCond %{QUERY_STRING} (^|&)access_token=988738467($|&)
RewriteRule ^(.*)$ http://tomcat-local:10022/$1 [QSA,P,L]
您可能希望啟用這些mod_proxy模塊mod_proxy_http。
編輯2:
您的配置應該是:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)access_token=988738467($|&)
RewriteRule ^(.*)$ http://tomcat-local:10022/$1 [QSA,P,L]
# disable some HTTP request types for security reasons
RewriteCond %{REQUEST_METHOD} ^TRACE
RewriteRule .* - [F]
RewriteCond %{REQUEST_METHOD} !^(GET|POST|HEAD|OPTIONS)$
RewriteRule .* - [F]
</IfModule>
確保 mod_rewrite 已啟用。我建議您在某天被禁用時洗掉IfModule以避免混淆。mod_rewrite因為IfModule不會導致錯誤出現,而且會造成混亂。
編輯3:
在虛擬主機之外添加RewriteOptions InheritDown,會導致規則被虛擬主機繼承。
因此,所有虛擬主機都將使用它。
默認情況下,在 Apache 中,在 virtualhosts 之外指定的規則不會被 virtualhosts 繼承。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/423064.html
標籤:
