Link to home
Start Free TrialLog in
Avatar of MOSTAGHASSI
MOSTAGHASSIFlag for United States of America

asked on

Redirect 301 from one address to another

Hello;

I want redirect  the page  details.php  with its URL parameters to page hdetails.php:

mydomain.com/health/details.php?articleid=8122&parentid=65&catid=87

Open in new window


redirect to:

mydomain.com/health/hdetails.php?articleid=8122&parentid=65&catid=87

Open in new window


thanks
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Regarding Julian's Option 2:

If you really want 301 moved permanently instead of 302 found, you need to send the 301 header redirection code in PHP.  PHP has its own ideas about your intention, and its assumption is that you really want a 302 instead of a 301.

You may also want to consider whether you want HTTP or HTTPS, but if you're redirecting within the same domain, that should happen automatically.
<?php
$loc = 'Location: /health/hdetails.php';
if ($_SERVER['QUERY_STRING']) $loc .= '?' . $_SERVER['QUERY_STRING'];
header($_SERVER["SERVER_PROTOCOL"] . " 301 Moved Permanently"); 
header($loc);

Open in new window


You might be able to do it this way (I've never tested it)
<?php
$loc = 'Location: /health/hdetails.php';
if ($_SERVER['QUERY_STRING']) $loc .= '?' . $_SERVER['QUERY_STRING'];
header($loc,TRUE,301);

Open in new window


Refs:
https://en.wikipedia.org/wiki/HTTP_301
https://en.wikipedia.org/wiki/HTTP_302
http://php.net/manual/en/function.header.php
Avatar of MOSTAGHASSI

ASKER

Thanks Julian,it works very well.
Thanks Ray for your comment and solutions.
The important difference between a 301 and a 302 will appear when you look at the SEO statistics.

For anyone coming to this question in the future, the accepted solution is missing a question-mark before the query string in Option 2.