Ad

How To Achieve A 302 Redirect After AJAX Post? Trying To Implement Prg-pattern

- 1 answer

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?

Ad

Answer

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

Ad
source: stackoverflow.com
Ad