Apache forwarding without redirecting (application won’t follow redirects)

apache-2.2mod-proxymod-rewrite

Recently we had to move /task to /public/task, and I'd like to configure Apache to redirect accordingly.

However, using mod_rewrite, though it works in the browser, seems to break applications making api calls to the above location. What happens is the application returns a page with the message saying the page was moved, but the app doesn't follow the redirect.

So, is there a way to simply forward any traffic to /task to /public/task without 'redirecting', i.e, returning a redirect status code?

EDIT: Here's a little more information. I've found a simple test to clarify what I'm trying to fix. Here is the URL path that needs forwarding:

https://mydomain.com/task

Needs to go to:

https://mydomain.com/public/task

If I use curl against the original domain, it just returns a redirect page notice. If I add the -L flag, which tells curl to follow redirects, it then follows the redirect successfully.

I assume something very similar is happening in the application (which I don't have access to) that makes calls to the /task URL path. Since I cannot modify the application to make it follow redirects properly, I'm looking for a solution I can implement in Apache.

EDIT 2:
Thanks everyone for you input. ryanm's suggestion of using the [P] flag in the rewrite worked. Here's everything that I did:

Apache has three ways apparently to forward or redirect vistors to a URL: Alias, Proxy and Rewrite.

Alias seems like the most obvious solution since it silently forwards the vistor to the new specified location. Unfortunately this didn't work for me becauce Aliases require specifying the new location on the actual filesystem, and with Jboss, that location changes at each redeploy/restart.

Using ProxyPass also seemed like it would work, and it possibly could, but every way I configured it, curl would return an empty result, and it didn't look like anything was happening. Here is the Proxy command I used to no avail:

ProxyPass /task http://localhost:8080/public/task

Finally, redirecting (with the [P] flag) fixed the problem. My initial redirect looked like this:

RewriteRule ^/task/(.*)$ public/task/$1 [R,L]

With the [R], Apache was returning redirect headers to the client, which worked fine in any browser, but caused applications like curl to stop a page that told them to redirect. Curl needed the -L flag to then follow the redirect.

Finally, using a combination of ProxyPass and Rewrite, I was able to get it to work. I used that [P] flag at ryanm's suggestion and curl would now follow the redirect without having to use the -L flag. My final directive looked like this:

RewriteRule ^/task/(.*)$ http://localhost:8080/public/task/$1 [P]

Thanks again everyone for the help!

Best Answer

I've used mod_alias successfully without giving the client a redirect; this was from a browser-requested URL /request/ to an internal directory /opt/myapplication.

However, if that solution does not work for some reason, have you considered a mod_rewrite proxy flag or a proxy setup? It's the same server, but if alias doesn't work, proxy may work to the localhost.

Related Topic