重定向index.php中的所有子URL

时间:2016-03-23 07:51:26

标签: php redirect subdirectory

我有以下网址结构:www.domain.com/test/anyString

我希望www.domain.com/test的所有请求都重定向到其他网站。但只有那些在他们的请求中有/测试/的人。

我能够通过www.domain.com执行重定向,但不能使用任何子目录。到目前为止我的代码:

//if our domain is called
if ($_SERVER['HTTP_HOST'] != "www.domain.com"){
header("HTTP/1.1 301 Moved Permanently");
$iPod    = stripos($_SERVER['HTTP_USER_AGENT'],"iPod");
$iPhone  = stripos($_SERVER['HTTP_USER_AGENT'],"iPhone");
$iPad    = stripos($_SERVER['HTTP_USER_AGENT'],"iPad");
$Android = stripos($_SERVER['HTTP_USER_AGENT'],"Android");
//$webOS   = stripos($_SERVER['HTTP_USER_AGENT'],"webOS");

//do something with this information
if( $iPod || $iPhone ){
//browser reported as an iPhone/iPod touch -- do something here
header("Location: https://itunes.apple.com/de"); exit; 
}else if($iPad){
//browser reported as an iPad -- do something here
header("Location: https://itunes.apple.com/de"); exit; 
}else if($Android){
//browser reported as an Android device -- do something here
header("Location: http://play.google.com/"); exit; 
}else{
header("Location: http://www.google.de"); exit; 
}
}

但这只适用于完整的主持人。

2 个答案:

答案 0 :(得分:1)

使用apache重写引擎,无需编写任何PHP代码,即可完成。请点击以下链接获取相同的

https://httpd.apache.org/docs/2.4/rewrite/remapping.html

例如:

  

上的RewriteEngine      

RewriteRule" ^ / text" " / someurl" [R]

答案 1 :(得分:0)

我测试了一下:

RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} "iphone|ipod|ipad" [NC]
RewriteRule ^(.*)test(.*)$ http://www.apple.com [R=301,L]

#redirect to android market
RewriteCond %{HTTP_USER_AGENT} "android" [NC]
RewriteRule ^(.*)test(.*)$ https://play.google.com [R=301,L]