1

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%20avenue%20des%20lilas&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

9
  • Welcome to SO, thanks for showing your efforts; could you please do confirm if you are hitting https://xxxdddyyy.dqco1.firma-online.com/rest/auto-com/complete_addr?in_country=FR&in_line=73 avenue des lilas&access_token=988738467 in your browser? And you want it to redirect/change it to URL http://tomcat-local:10022/? Thank you. Commented Jan 27, 2022 at 8:08
  • I just now noticed that my description is not quite correct, The URL I want to redirect can consist of various parameters, my match has to work only on this particular bearer token which is at the end of Get Request. https://xxxdddyyy.dqco1.firma-online.com/rest/auto-com/complete_addr?in_country=(.*)&in_line=(.*)&access_token=988738467 this is what it should look like from a regex perspective all this should be redirect to http://tomcat-local:10022/rest/auto-com/complete_addr?in_country=$1&in_line=$2&access_token=988738467 Commented Jan 27, 2022 at 8:34
  • Sorry its still not clear, please do mention 2 simple urls. 1 which you are hitting in browser/postman and 2nd which you want it to change in that browser, that will help me to understand question more clearly, thank you. Commented Jan 27, 2022 at 8:42
  • So maybe I'll start from the beginning, my goal is for apache to be able to grab an url with a specific baerer Token and send it unchanged to a specific instance of tomcat where it will be served. https://xxxdddyyy.dqco1.firma-online.com/rest/auto-com/complete_addr?in_country=(.*)&in_line=(.*)&access_token=988738467 How to catch only a match on a specific baerer token and forward the whole request. The goal is that a client with a specific token will be served by a specific tomcat server. Commented Jan 27, 2022 at 8:52
  • What I mean from simple urls is: example you are hitting url like: http://localhost:80/test?test_blabla and you want it to change to http://localhost:80/test etc. Commented Jan 27, 2022 at 8:52

1 Answer 1

3

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:

  1. https://xxxdddyyy.dqco1.firma-online.com/rest/auto-com/complete_addr?in_country=FR&in_line=73 avenue des lilas&access_token=988738467 to http://tomcat-local:10022/rest/auto-com/complete_addr?in_country=FR&in_line=73 avenue des lilas&access_token=988738467 if access_token is 988738467
  2. https://xxxdddyyy.dqco1.firma-online.com/rest/auto-com/complete_addr?abc=123&access_token=988738467 to http://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:

  1. ^access_token in ^access_token=(988738467)$ means that access_token must be the first in the query string.
  2. (988738467)$ means that 988738467 must 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.

Sign up to request clarification or add additional context in comments.

10 Comments

I usually do such redirections with ProxyPass, but the documentation said that if I use parameters I should use rewrite, ProxyPass does not support advanced regex, as far as I know
@Pawel, oh so you want to redirect internally?
@Pawel, I have added a solution for internal redirect to proxy. See under the "Edit" section in my answer
Yes, the redirection must be internally
@Pawel "I have the impression that none of the regexes in RewriteCond work", Oh, I get it now. Add RewriteEngine On to all virtualhosts, and add RewriteOptions InheritDown to the above rule.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.