0

I am trying to redirect a webpage depending on the URL parameters. I have gone through so many tutorials and stackflow answers in the last couple of days without finding a way past this. I am not a programmer but learnt everything i know about php and html coding using stackflow.So thanks you everyone.

www.domain.com/women/product?id=08607080

would like to redirect to

  www.domain.com/women/Accessories/product?id=08607080

this is the code I am using now but it directs to www.dmail.com/women

Help please

<?php
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/womenconfig.inc.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.inc.php';

mysql_connect(HOST, USERNAME, PASSWORD) or die(mysql_error());
mysql_select_db(DB) or die(mysql_error());

$ArtNumber = isset($_GET['id'])?$_GET['id']:Null; 

$ArtNumber = isset($_GET['id'])?$_GET['id']:Null; 
$query = "(SELECT * FROM " . implode(' WHERE ArtNumber=\'' . $ArtNumber . '\') UNION (SELECT * FROM ', array_keys($shops_arr)) . '  WHERE ArtNumber=\'' . $ArtNumber . '\' ) ';

$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);


    $ArtNumber  = $row['ArtNumber'] ;
    $ProductCategory1   = $row['ProductCategory1'] ;
    $ProductCategory2   = $row['ProductCategory2'] ;
    $ProductCategory3   = $row['ProductCategory3'] ;



    echo "<script>window.location = ''/women/Accessories'.$ProductCategory1.'/product?id=',$ArtNumber,''</script>";

?>

4
  • could you post line window.location = ... which is rendered when you execute the script? I suspect not all parameters are passed. Commented Jan 25, 2015 at 12:32
  • 1
    You should not redirect like this... Either use the propper headers() function or directly use the rewriting module of your http server. Commented Jan 25, 2015 at 12:32
  • echo "<script>window.location = ''/women/Accessories'.$ProductCategory1.'/product?id=',$ArtNumber''</script>"; Commented Jan 25, 2015 at 12:34
  • at arkascha as I said I not good at these things Commented Jan 25, 2015 at 12:34

2 Answers 2

1

As @arkassha said you should set up proper HTTP header (Location: url) or use web server capabilities. Here is example of redirection via location header:

<?php
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/womenconfig.inc.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.inc.php';

mysql_connect(HOST, USERNAME, PASSWORD) or die(mysql_error());
mysql_select_db(DB) or die(mysql_error());

$ArtNumber = isset($_GET['id'])?$_GET['id']:Null; 

$ArtNumber = isset($_GET['id'])?$_GET['id']:Null; 
$query = "(SELECT * FROM " . implode(' WHERE ArtNumber=\'' . $ArtNumber .    '\') UNION (SELECT * FROM ', array_keys($shops_arr)) . '  WHERE ArtNumber=\'' .   $ArtNumber . '\' ) ';

$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);


$ArtNumber  = $row['ArtNumber'] ;
$ProductCategory1   = $row['ProductCategory1'] ;
$ProductCategory2   = $row['ProductCategory2'] ;
$ProductCategory3   = $row['ProductCategory3'] ;


header("Location: /women/Accessories/product?id=$ArtNumber");
?>

One thing to note here that headers must be sent before any contents.

As of second variant in case of apache you could use rewrite rules: http://kb.mediatemple.net/questions/85/Using+.htaccess+rewrite+rules#gs

It is also possible to redirect via javascript as you mentioned but that is not the preferred solution.

Sign up to request clarification or add additional context in comments.

2 Comments

not working getting this errorPHP Parse error: syntax error, unexpected '='
works fine but can I use like this - header("Location: /women/'.$ProductCategory1.'/product?id=$ArtNumber"); so the the Accessories word will come from $ProductCategory1
1
 window.location.replace(" ''/women/Accessories'<?php echo "{$ProductCategory1}'/product?id={$ArtNumber}'' "; ?>");

please try this.

2 Comments

Did not work coming with the following error PHP Parse error: syntax error, unexpected '{' in
@Prakash please check proper brackets

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.