[英]Redirect old website with parameters to new website with parameters under the same domain
[英]Redirect to a new domain based on GET parameters
我已經多次使用301重定向,但這次我遇到了一個新問題。 我在Joomla上有一個舊網站,網站上的所有頁面都由相同的index.php
文件加載。 示例:
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
如何將每個頁面重定向到其他頁面?
例如,我想將頁面http://domain.com/index.php?option=com_content&view=section&id=4&Itemid=54
重定向到http://newdomain.com/location/
我需要根據GET參數進行301重定向,但我無法弄清楚如何。
非常感謝您的幫助!
如果可以在htaccess
文件中執行此操作?
使用簡單的開關
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}");
當然你可以輕松添加更多參數並切換它們......就像
if($_GET['id'] == 1 && $_GET['item'] == 55 ){
$goto = 1;
}
switch($goto){ ... }
htaccess會有些像
RewriteEngine On
Redirect 301 /index.php?option=com_content&view=section&id=4&Itemid=54 http://www.domain.com/
通過GET
發送的參數將作為以下結構接收:
array(n):
name_1 = 'value1',
name_2 = 'value2',
...
name_n = 'valueN',
您可以通過$_GET
變量訪問它們,其中每個鍵都等於參數的名稱,例如,要訪問綁定到名稱為'foo'
的參數的值,您可以:
$bar = $_GET['foo'];
在你的PHP腳本上。 因此,您只需創建一個簡單的流控制器或一個有效檢測通過GET
發送的參數並重定向到您正在尋找的URL的開關 :
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>':
...
}
$parameters
參數是可選的,您可以使用它通過GET
將一些其他數據發送到新URL,您還可以控制通過可選的$statusCode
參數發送的狀態代碼。
你可以使用這樣的規則你的DOCUMENT_ROOT/.htaccess
文件:
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]
確保將這些規則放在任何其他規則之前。
之后的字符串?
在URL中稱為查詢字符串。 在PHP中,它包含在QUERY_STRING環境變量中。 因此,您可以使用index.php腳本中的if
語句來執行您想要執行的操作,以測試查詢字符串的內容,如下所示:
if($_SERVER['QUERY_STRING']=="option=com_content&view=section&id=4&Itemid=54") { header("Location: http://newdomain.com/location/"); }
使用htaccess我相信你可以做到這一點。
這應該工作:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com$
RewriteRule (.*)$ http://www.newdomain.com/$1 [R=301,L]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.