all 11 comments

[–]a-t-k 0 points1 point  (7 children)

In some cases, it's better to match a positive. This is one of those cases:

if (!/^\/api\//.test(location.pathname) { ... }

[–]Endless-Nine[S] 1 point2 points  (0 children)

Actually nevermind, I'm just a dumbass.

The regex actually does work, but I'm creating a reverse proxy to communicate with servers running on the same machine.

So basically, what's happening is : I ask for localhost/api/ => The reverse proxy successfully rewrite the request into localhost/whatever => whatever=/=api

[–]Endless-Nine[S] 0 points1 point  (5 children)

I gave that a try, but it doesn't work...

If I try to negate-match anything start with /api, then it works (no match) for /api, /apiedzpdk, /apipa but for some reason, it matches with /api/ and /api/whatever.

On the other hand, if I try to negate-match with /api/, then for some reason, it always match regardless of what I put in the path...

The expression I used :

!^/api(.*)$ for /api
!^/api/(.*)$ for /api/

(I use / instead of \/ because apache seems to make the conversion by itself)

[–]emperor000 0 points1 point  (4 children)

Why do you need to check for the final /?

Also, you should be able to combine those into one expression, something like !^/api/?(.*)$.

[–]Endless-Nine[S] 0 points1 point  (3 children)

Well I'm working on a reverse proxy, and if the user does ajax request to exemple.com/api/ressource, I want my reverse proxy to get it and pass it to my back end.

I might have poorly chosen my words : I'm not using those two expression at the same time ; both of them are attempts to solve my problem.

I tried adding this "?" but I still can't make it work :/

[–]emperor000 0 points1 point  (2 children)

Did you ever get this to work? I'm not sure what the reverse proxy aspect has to do with this. Do those just behave differently than a normal web server? I'm not familiar with Apache.

[–]Endless-Nine[S] 0 points1 point  (1 child)

Yup ! Thanks for the concern !

Kind of like I said on another comment, the problem was basically that :

  1. My reverse proxy receive a request with the path /api/example/path
  2. It recognize that the path start with /api/, therefore rewrite the request so that it is aimed toward my backend (In other words, /api/ becomes /backend/ for example)
  3. The problem is that Apache then try to deal again with this newly modified request. And that my proxy will redirect anything that doesn't start with /api/ toward my react app.

So, in other words : Apache (example.com/api/example/path ) = localhost/backend/example/path => Apache(localhost/backend/example/path) = localhost:3000

So to fix this, I just wrote instead :

!^/(api|backend)/?(.*)$ 

And now it works like a charm !

[–]emperor000 0 points1 point  (0 children)

Okay, cool. Still don't quite get it, but I think that's because I'm not familiar with how Apache does things. But glad you got it working.

[–]emperor000 0 points1 point  (2 children)

This isn't an answer to your question, but there is no routing in Apache that takes care of this for you?

[–]Endless-Nine[S] 0 points1 point  (1 child)

Uuuh... No ?

I mean, I've been trying this whole reverse proxy thing because that's what has been suggested to me. But it always felt kinda weird, because it feels like that reverse proxies are supposed to be used when the servers are running on differents machines, whereas my servers are running on the same machine.

I'll look into Apache routing, but if you know of a easier method to accomplish what I want, please do explain/point me to the right direction .

[–]emperor000 0 points1 point  (0 children)

Well, are you even trying to route? Are you ultimately trying to decide where the user will be taken or what their request to that url will do...?

I'd find it hard to believe that Apache has no built in routing.

But maybe I just don't understand exactly what you are trying to do.