Jun Wei
Jun Wei

Reputation: 31

i need to redirect all page except for 1 page with POST data

I need to redirect requests to my old domain to a new domain while retaining a page in the old domain.

Edit: In short all .json with post data get redirect to new domain except for 1 page

How can i get it done, if possible at all? I use fiddler to debug in raw view

Before redirect (with .htaccess method)

POST http://old.com/data.json HTTP/1.1
Content-Type: application/x-www-form-urlencoded
User-Agent: Dalvik/1.6.0 (Linux; U; Android 4.1.2; GT-N7105 Build/JZO54K)
Host: old.com
Connection: Keep-Alive
Accept-Encoding: gzip
Content-Length: 100

session=gVgr30Erxf03cDaA&store_version=9203b&inventory_version=123

After redirect

GET http://new.com/data.json HTTP/1.1
Content-Type: application/x-www-form-urlencoded
User-Agent: Dalvik/1.6.0 (Linux; U; Android 4.1.2; GT-N7105 Build/JZO54K)
Host: new.com
Connection: Keep-Alive
Accept-Encoding: gzip

Upvotes: 0

Views: 207

Answers (2)

Arnold Daniels
Arnold Daniels

Reputation: 16563

Use a .htaccess with mod_rewrite

RewriteEngine On
RewriteRule ^page\.json$  -                        [L]
RewriteCond %{REQUEST_METHOD} =POST
RewriteRule ^(.*\.json)$  http://newdomain.com/$1  [R=307,QSA]
  • The 2nd line says: Don't change if the page matches 'page.json' and stop rewriting [L].
  • The 3th line says: Only do rewrite POST requests
  • The 4th line says: Math anything that ends on '.json' and to a temporary redirect [R=307] to http://newdomain.com/{page}. Also add any URL parameters [QSA].

Proof of concept

.htaccess

RewriteEngine On
RewriteCond %{REQUEST_METHOD} =POST
RewriteRule ^$  http://test.jasny.net/redirect/posthere.php  [R=307,QSA]

index.html

<form action="/redirect/" method="POST">
  <input type="text" name="test">
  <button type="submit">Submit</button>
</form>

posthere.php

<?
  var_dump($_POST);

See it work

Upvotes: 1

Momen Zalabany
Momen Zalabany

Reputation: 9007

i agree with arnold, .htaccess, but if u want ONLY requests with $_POST data then i would create a small php file that handle request, test if if it contain post then redirect to ur old domain, if not redirect to new domain

.htaccess

RewriteCond %{REQUEST_METHOD} =POST
RewriteRule ^foo/bar$ http%1://www.example.com%{REQUEST_URI} [L,R=301]

or redirect all traffic to php file and do:

if( count( $_POST ) < 1 ) {
    header( "HTTP/1.0 301 Moved Permanently" ); 
    header( "Status: 301" );
    header( "Location: http://".CLIENT_DOMAIN."{$_SERVER['REQUEST_URI']}");     
    die();//or exit;
}else{////same}

Upvotes: 0

Related Questions