Welcome,
for a long time I'm struggling with the problem of redirecting the url to a separate url in case the a special baerer token is given, I don't want to change any parameter just make a match and send this match to another url.
My setup is apache 2.4.41
typical request from postman:
https://xxxdddyyy.dqco1.firma-online.com/rest/auto-com/complete_addr?in_country=FR&in_line=73 avenue des lilas&access_token=988738467
the redirection should be like this:
http://tomcat-local:10022/rest/auto-com/complete_addr?in_country=FR&in_line=73 avenue des lilas&access_token=988738467
What I've tried so far in apache2.conf
RewriteCond %{QUERY_STRING} ^access_token=(988738467)$
RewriteRule (.*) http://tomcat-local:10022/ [R=301,L]
The goal is that a client with a specific token will be served by a specific tomcat server
Please help me, maybe I'm doing something wrong, I tried also RedirectMatch, unfortunately without success, all regexes I tested on https://regex101.com/
EDIT:
Still Nothing,I don't understand what's wrong. My config
<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>
Access_log :
"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"
Solved
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]
Greetings Paul
CodePudding user response:
Finally this issue was solved by adding RewriteOptions InheritDown before the rewrite rules, and adding RewriteEngine On to all the virtualhosts.
Try:
RewriteCond %{QUERY_STRING} (^|&)access_token=988738467($|&)
RewriteRule ^(.*)$ http://tomcat-local:10022/$1 [QSA,R=301,L]
This will redirect:
https://xxxdddyyy.dqco1.firma-online.com/rest/auto-com/complete_addr?in_country=FR&in_line=73 avenue des lilas&access_token=988738467tohttp://tomcat-local:10022/rest/auto-com/complete_addr?in_country=FR&in_line=73 avenue des lilas&access_token=988738467ifaccess_tokenis988738467https://xxxdddyyy.dqco1.firma-online.com/rest/auto-com/complete_addr?abc=123&access_token=988738467tohttp://tomcat-local:10022/rest/auto-com/complete_addr?abc=123&access_token=988738467
Although I gave you this solution, I am still very confused on why you want to do an external redirect to a local page. I believe you want a proxy pass.
Explanation why the Rewrite* on the question didn't work:
^access_tokenin^access_token=(988738467)$means thataccess_tokenmust be the first in the query string.(988738467)$means that988738467must be at the end of the query string.
With the above issues, only https://https://xxxdddyyy.dqco1.firma-online.com/anything?access_token=988738467 will work with your RewriteCond.
Edit:
For internal redirect to backend server:
RewriteCond %{QUERY_STRING} (^|&)access_token=988738467($|&)
RewriteRule ^(.*)$ http://tomcat-local:10022/$1 [QSA,P,L]
You might want to have the modules mod_proxy and mod_proxy_http enabled.
Edit 2:
Your configuration should be:
<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>
Make sure mod_rewrite is enabled. I suggest you to remove IfModule to avoid confusion when some day mod_rewrite is disabled. Because IfModule will cause no error to show up, and it will cause confusion.
Edit 3:
Adding RewriteOptions InheritDown outside the virtualhosts, causes the rules to be inherited by the virtualhosts.
So, all the virtualhosts will be using it.
By default in Apache, the rules specified outside virtualhosts is not inherited by virtualhosts.
