How to redirect HTTP post request

11,091

Solution 1

Looks like Apache config? It's a good idea to specify things like that. The tags confirm it though.

That config only runs when they connect on port 443, so it can't redirect from HTTP.

You can't do a 30[12] redirect in response to a POST request and keep the arguments unless you convert the request to a GET and write the arguments into the URL. Not really recommended.

You can proxy the request, but I'm not sure that solves your problem.

If the user has already submitted data via POST over an unencrypted connection, and you care about the encryption, you're probably best to let that request break anyway, so it gets noticed and fixed. You should be fixing your form target, and also making sure that the form itself (or the page with AJAX in it or whatever) is sent to the user over HTTPS.

UPDATE

Given that shawsy has said that the problem is that The browser cannot make HTTPS connections to the server, a redirect is definitely not what's wanted. Rather you want to proxy the request:

<VirtualHost 10.1.2.91:80>
  # http://httpd.apache.org/docs/2.2/mod/mod_proxy.html#proxypass
  ProxyPass /GladQE/link https://glad-test.com/GladQE/link.do
  ProxyPass /GladQE/retrieve https://glad-test.com/GladQE/retrieve.do
</VirtualHost>

You could alternatively do it with mod_rewrite and RewriteRule.

There are some extra issues to work out if you're changing the domain name, but I think that's not the case here.


Just as an aside, I personally don't like putting host names or IP addresses anywhere except in the server's /etc/hosts file. If you use names in the hosts file like 'web' and 'mysql', which locate services rather than machines, and you refer to those in your apache and other files, then you can move configuration between machines much more easily, knowing that you only have to go over what's in the hosts file.

Solution 2

You can use 307 redirect to keep method. From Wikipedia:

In this case, the request should be repeated with another URI; however, future requests should still use the original URI. In contrast to how 302 was historically implemented, the request method is not allowed to be changed when reissuing the original request. For example, a POST request should be repeated using another POST request.

Share:
11,091
Cip
Author by

Cip

Updated on September 18, 2022

Comments

  • Cip
    Cip over 1 year

    Hi I am currently using the below code in my apache httpd.conf file to redirect from HTTP to HTTPS

    <VirtualHost 10.1.2.91:80>
    Redirect 302 /GladQE/link https://glad-test.com/GladQE/link.do
    Redirect 302 /GladQE/retrieve https://glad-test.com/GladQE/retrieve.do
    </VirtualHost>
    

    This redirects parameters from a get request but not from a post. From reading on here it looks like this needs to be done using mod_rewrite.

    Can somebody help with the amendments I would need to make so that when the link on the left is hit using post params it redirects to the link on the right with the params intact?

    Many thanks

    Tom

  • Cip
    Cip almost 11 years
    Yes, Sorry I didnt mention the code is from httpd.conf file. I also made a mistake on the virtual host tag. I have now ammended to port 80 not 443.
  • mc0e
    mc0e almost 11 years
    Then a redirect is definitely not what you want.
  • Cip
    Cip almost 11 years
    Do I need to have the mod_proxy.so file in the modules folder? The only files I have in the modules folder are httpd.exp and mod_jk.so
  • mc0e
    mc0e almost 11 years
    you will need mod_proxy. How you go about configuring that varies a bit between different operating systems and distributions.
  • Cip
    Cip almost 11 years
    ok, I now have the mod_proxy module in my build of apache. DO you know how I add this in the https.conf file? Currently when I use Proxy Pass when starting apache i get the message "Invalid command, ProxyPass"
  • Cip
    Cip almost 11 years
    If I point the proxypass to http it works fine. if i point to https then get 500 error and this message in the logs.[warn] proxy: No protocol handler was valid for the URL /LiabilityQE/redirect. If you are using a DSO version of mod_proxy, make sure the proxy submodules are included in the configuration using LoadModule.
  • jira
    jira over 6 years
    The best answer.