Reputation: 412
I already used 301 redirect many times, but this time I'm facing a new problem. I have an old website on Joomla and all the pages on the website are loaded by the same index.php
file. Example :
URL of a page : http://domain.com/index.php?option=com_content&view=section&id=4&Itemid=54
URL of a different page : http://domain.com/index.php?option=com_content&view=section&id=1&Itemid=55
How can I redirect each of these pages to other pages?
By example, I'd like that the page http://domain.com/index.php?option=com_content&view=section&id=4&Itemid=54
redirect to http://newdomain.com/location/
I need to make a 301 redirect based on GET parameters but I can't figure how.
Your help would be very appreciated !
If possible to do this in the htaccess
file?
Upvotes: 1
Views: 433
Reputation: 785481
You can use rules like this your DOCUMENT_ROOT/.htaccess
file:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^option=com_content&view=section&id=4&Itemid=54$
RewriteRule ^(index\.php)?$ http://newdomain.com/location/? [L,NC,R=302]
Make sure these rules are placed before any other rules.
Upvotes: 1
Reputation: 6565
use a simple switch
switch($_GET['id']){
case 1: $myPage = "http://newdomain.com/location/"; break;
case 4: $myPage = "http://othernewdomain.com/location/"; break;
default: $myPage = "http://google.com"; break;
}
header("Location: {$myPage}");
of course you could easy add more parameters and switch them... like
if($_GET['id'] == 1 && $_GET['item'] == 55 ){
$goto = 1;
}
switch($goto){ ... }
htaccess would be some like
RewriteEngine On
Redirect 301 /index.php?option=com_content&view=section&id=4&Itemid=54 http://www.domain.com/
Upvotes: 2
Reputation: 9145
The parameters that are sent through a GET
are received as a structure like this:
array(n):
name_1 = 'value1',
name_2 = 'value2',
...
name_n = 'valueN',
And you can access them through the $_GET
variable, where every key equals to the name of the parameter, e.g. to access the value bound to the parameter with the name 'foo'
, you can:
$bar = $_GET['foo'];
On your PHP script. So you can just make a simple flow controller, or a switch that effectively detects the parameters sent through the GET
and redirects to the URL you're looking for:
final class Redirect {
public static function toUrl($url, $parameters = array(), $statusCode = 303) {
header('Location: ' . $url . http_build_query($parameters), true, $statusCode);
}
}
$parameter = $_GET['key'];
switch ($parameter) {
case '<the specific parameter>':
Redirect::toUrl('http://example.com/');
break;
case '<other case>':
...
}
The $parameters
argument is optional and you can use it to send some other data to the new URL via GET
too, and you can also control the status code sent via the optional $statusCode
parameter.
Upvotes: 1
Reputation: 12027
The string after the ?
in the URL is called the query string. In PHP, it's contained in the QUERY_STRING environment variable. So, you can do what you want to do using if
statements in your index.php script to test the contents of the query string, like so:
if($_SERVER['QUERY_STRING']=="option=com_content&view=section&id=4&Itemid=54") { header("Location: http://newdomain.com/location/"); }
Upvotes: 0
Reputation: 11942
Using htaccess I believe you can do that.
This should work :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com$
RewriteRule (.*)$ http://www.newdomain.com/$1 [R=301,L]
Upvotes: -1