0

I am trying to implement a "post request get" pattern for SEO reasons like explained in this example: https://www.advertising.de/seo/wiki/prg-pattern.html

My implementation consits of a pseudo link element, styled via CSS like a link:

<span data-prg="redirect_url.html" class="btn redir-link" title="test">PRG Link test</span>

A jquery AJAX POST call

<script type="text/javascript">
    var request;
    $(function() {
        $('.redir-link[data-prg]').click(function (e) {
            var $self = $(this);
            request = $.ajax({
                url: "/prg.php",
                type: "post",
                data: {
                    url: $self.data("prg")
                }
            });
        });
    });
</script>

Th PHP file that will create the redirect URL and which should 302 redirect via GET

header("Location:".$_POST['url'], true, 302);
exit;

The apache log states that the implementation works:

"POST /prg.php HTTP/1.1" 302 "GET /redirect_url.html HTTP/1.1" 200

However the page inside the browser does not change. I suspect that the php file that is called via ajax will change but this is not what I want. The orginial page should be redirected.

How could I accieve a 302 redirect for the original page?

Cœur
  • 37,241
  • 25
  • 195
  • 267
merlin
  • 2,717
  • 3
  • 29
  • 59
  • Possible duplicate of [How to access property with jquery](http://stackoverflow.com/questions/40376016/how-to-access-property-with-jquery) – madalinivascu Nov 02 '16 at 10:46
  • The original page won't be redirected because you're not loading a "page", you're loading *data* via ajax. Ajax, by definition, does not affect the whole page. It may be that the data returned is html, but it's not a browser load-302-redirect. The PRG pattern is "post-redirect-get" (not -request- as in the question). If you want the entire page to change, you'll need to add a `location.href=` in success/done. One way to handle this is to return a response object that includes the new url. – freedomn-m Nov 02 '16 at 11:02

1 Answers1

1

You can't redirect in a ajaxed page the page isn't available for the user,do a simple form that will trigger a post request to /prg.php in prg you then redirect back

madalinivascu
  • 32,064
  • 4
  • 39
  • 55