2012-11-16 84 views
0

我寫了一些PHP,檢測用戶是否使用移動設備或計算機訪問我的博客文章。jQuery/Javascript - 動態插入文本到URL

基本上,我需要一個腳本,如果用戶使用移動設備訪問我的博客帖子,則會動態編輯URL。

例如,我的博客文章的網址是這樣的:

http://www.example.com/blog/blog-post-1/
http://www.example.com/blog/welcome-to-my-blog/
http://www.example.com/blog/another-blog-post/

用戶應該動態地重定向到:

http://www.example.com/m/blog/blog-post-1/
http://www.example.com/m/blog/welcome-to-my-blog/
http://www.example.com/m/blog/another-blog-post/

我也有設備檢測在PHP中工作,所以我只需要腳本到PHP條件內插入。

任何想法?

+0

這應該被標記爲'php'問題,可能是'htaccess'問題,但不是'javascript'或'jquery'問題。 – madlee

+0

好的,對不起,我不知道這樣做的最好方法 –

回答

2

嘗試:

if ($mobile) { 
    header("Location: http://".$_SERVER["HTTP_HOST"]."/m".$_SERVER["REQUEST_URI"]); 
} 
0

這的.htaccess片段可能有助於實現你希望做什麼:

這種重寫覆蓋了大多數的移動設備(除了少數誤報)。當檢測到移動設備時,它將被重定向到/ m /。

http://snipplr.com/view/44741/

0

如果您已經使用PHP做設備檢測,這應該這樣做:

header("Location: /m{$_SERVER['REQUEST_URI']}"); 
exit; 

它採用當前路徑和它的前綴與"/m"形成移動路徑。

使用JavaScript進行重定向會排除某些移動設備,因此這可能不是最好的主意。

0

這應做到:

$服務器

<?php 
echo $_SERVER['HTTP_USER_AGENT']; 
?> 

user_agent.php:

<?php 
$iphone = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone"); 
$android = strpos($_SERVER['HTTP_USER_AGENT'],"Android"); 
$palmpre = strpos($_SERVER['HTTP_USER_AGENT'],"webOS"); 
$berry = strpos($_SERVER['HTTP_USER_AGENT'],"BlackBerry"); 
$ipod = strpos($_SERVER['HTTP_USER_AGENT'],"iPod"); 

if ($iphone || $android || $palmpre || $ipod || $berry == true) 
{ 
header('Location: http://mobile.site.com/'); 
//OR 
echo "<script>window.location='http://mobile.site.com'</script>"; 
} 
?> 

的index.php:

<?php 
include('user_agent.php'); // Redirecting http://mobile.site.info 
// site.com data 
?> 

內容從this網站。