简体   繁体   中英

index.php won't recognize route when fetching from node

I'm build kind of a proxy in Nodejs for a payment gateway to connect with an application I have in PHP.

I'm trying now to hit an endpoint in my PHP application from my node server with a GET request, but I keep getting the same error "Too Many Redirects".

Searching Google I found out that this is error usually has to do with the rewrite module in my .htaccess file, that might be endlessly redirecting my route, never falling out of the rewrite conditions.

Taking a look at my .htaccess file it seems pretty boilerplate and I don't know what could be causing this infinite redirect loop.

<IfModule mod_rewrite.c>
  RewriteEngine On
  # !IMPORTANT! Set your RewriteBase here and don't forget trailing and leading
  #  slashes. If your page resides at http://www.example.com/mypage/test1 then use RewriteBase /mypage/test1/
  RewriteBase /
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
  # If we don't have mod_rewrite installed, all 404's can be sent to index.php, and everything works as normal.
  # Submitted by: ElliotHaughin
  ErrorDocument 404 /index.php
</IfModule>

The weirdest of all is if I try exactly the same route using Postman or the browser everything works fine! The controller is hit and I get my response.

Also, on further research, I manage to log the request object in my index.php file for a working request from Postman and the failing one from my nodejs server. They seem pretty much the same in almost all fields, except in the fields describing the URLs. In the success request I get the following fields:

   // this is logged once
   [REDIRECT_QUERY_STRING] => /notifica
   [REDIRECT_URL] => /notifica
   [QUERY_STRING] => /notifica
   [REQUEST_URI] => /notifica

But when fetching from nodejs these are the fields I get:

   // this is logged many times until I reach my redirect limit
   // [REDIRECT_QUERY_STRING] doesn't exist
   // [REDIRECT_URL] doesn't exist
   [QUERY_STRING] =>
   [REQUEST_URI] => /

Does anyone know what could be causing this behavior? Why would a request work from Postman and the browser but not from an application in Nodejs? Maybe I'm missing something in the headers the two programs add by default but the modules I use to make the requests don't?

Your main rule always matches everything and always modifies the URL. Understand that the L flag simply stops looking for more matching rules in the current iteration, and it loops again with the rewritten URL. See the explanatory diagram of what rewriting actually does. In simple cases like yours, using the END flag instead is enough, otherwise your rule must both match the initial request and not match the rewritten URL.

PS: Don't use GET requests for anything that makes changes, that's what POST, etc. are for.

PPS You don't need to add the path to a query string, PHP should be able to see the request in $_SERVER['REQUEST_URI']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM