301基于POST中的变量重定向

时间:2014-01-17 16:44:33

标签: regex .htaccess redirect

所以我需要将某些搜索POST发送到不同的网址...

www.xyz.com/search-results.php?city%5B%5D=Boynton+Beach&proptype=rental&foreclosure=&price_from=0&price_to=0&footage_from=&footage_to=&bedrooms_from=&bedrooms_to=&bathrooms_from=&bathrooms_to=&pets=&year_from=&year_to=&pool=

在这种情况下,我需要解析proptype=rental并将整个事情发送到

www.xyzRentals.com/search-results.php?city%5B%5D=Boynton+Beach&proptype=rental&foreclosure=&price_from=0&price_to=0&footage_from=&footage_to=&bedrooms_from=&bedrooms_to=&bathrooms_from=&bathrooms_to=&pets=&year_from=&year_to=&pool=

我知道我需要添加一些正则表达式来解析.htaccess文件中的url并执行301 ...任何快速的想法如何完成这项工作?我试过了

RedirectMatch 301 ^search-results.php?(.*)proptype=rental(.*)$   http://www.xyzRentals.com/search-results.php?$1proptype=rental$2

但没有工作,我不知道为什么......当我把正则表达式放到匹配器中时,它表示它匹配但仍然没有重定向。

ANSWER

所以我在jquery中结束了...

var searchType = $('#proptype').val();

if (searchType == 'rental') {
   $('#form').attr('action','http://www.floridaspropertymanagement.com/search-results.php');
}
$('#form').submit();

2 个答案:

答案 0 :(得分:0)

header("Location: [...]);发出301 -

所以如果你的$ _POST变量总是proptype =那么,

header("Location: {$yourURLwithLotsOfGETparams}&proptype={$_POST['proptype']});

答案 1 :(得分:0)

Quick'n'dirty

$str = 'www.xyz.com/search-results.php?city%5B%5D=Boynton+Beach&proptype=rental&foreclosure=&price_from=0&price_to=0&footage_from=&footage_to=&bedrooms_from=&bedrooms_to=&bathrooms_from=&bathrooms_to=&pets=&year_from=&year_to=&pool=';

preg_match('/proptype=([^&]*)/i', $str, $matches);
$host = "";
if (isset($matches[1])) {
    switch ($matches[1]) {
        case 'rental':
        $host = 'www.xyzRentals.com';
    break;
    }
}

$str=preg_replace('#([^\/]*)\/#i', $host . '/', $str);


header("Location: $str");