merlin merlin - 1 year ago 94
PHP Question

How to achieve a 302 redirect after AJAX post? Tying to implement prg-pattern

I am trying to implement a "post request get" pattern for SEO reasons like explained in this example:

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: $"prg")

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

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

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?

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download