我嘗試在我的網站上實現pushstate歷史記錄,以便從index.php容器內的single.php頁面加載內容。HTML5 pushstate和SEO鏈接
我的網站有兩個主頁:index.php和single.php。
在index.php有調用pushState的腳本鏈接:
<a class="phplink" href="/Formlabs-3D-printer" title="Formlabs 3D printer">Post 12</a>
<a class="phplink" href="/Minimal-Bumper-for-iPhone-5" title="Minimal Bumper for iPhone 5">Post 11</a>
在我的single.php頁我用isset get方法來動態加載對應於點擊的index.php鏈接內容:
<?php
if (isset($_GET["page"])) {
//I do some stuff in order to echo content
?>
在我的.htaccess文件我重寫URL鏈接(其原因是,在index.php文件鏈接是清洗):
Options +FollowSymLinks
RewriteEngine on
RewriteRule /([a-zA-Z0-9\-]+)$ /index.php
RewriteRule /([a-zA-Z0-9\-]+)$ /single.php?page=$1 [L]
這裏我pushState的腳本:
$.ajaxSetup({cache:false});
$(".phplink").click(function(){
var $post_link = $(this);
load_content($post_link.attr('title'),$post_link.attr('href'));
return false;
});
window.onpopstate = function(event) {
if (event.state) {
load_content(event.state.title, window.location.pathname, true);
} else {
var stateObj = {
title: document.title,
url: window.location.pathname,
};
url = window.location.pathname;
load_content(url,url);
}
}
function load_content(title,url,skipHistory) {
$.get(url,function (data) {
document.title = title;
var stateObj = {
title: title,
url: url
};
if (!skipHistory) {
if (typeof window.history.pushState == 'function') {
window.history.pushState(stateObj,title,url);
}
}
if(url.substring(1) != '') {
$("#ajaxify_container").html("loading...");
$("#ajaxify_container").load('single.php?page='+url.substring(1)+' #container-2');
}
else {
$("#ajaxify_container").html('');
}
});
}
我pushState的腳本作品上加載鏈接點擊的內容(上.phplink
點擊)。 它也適用於後退/前進按鈕。
1st問題:當我刷新瀏覽器(在url中使用pushstate鏈接)時,它適用於谷歌瀏覽器(從single.php加載內容到index.php容器),但在IE10中沒有(沒有加載任何東西,它停留在index.php頁面)。
第二個問題:如果我禁用JavaScript以查看googlebot(針對SEO)會發生什麼情況。我無法加載/到達single.php頁面,它始終停留在index.php上。所以single.php頁面不能被搜索引擎抓取(我想,但我不確定)。 這種行爲是正常的,因爲我在我的.htaccess文件中設置了「所有這些鏈接」將重定向到index.php。
我這樣做,因爲沒有它pushState加載single.php頁面時,我刷新。我不想要這種行爲。我想刷新它時,只需將single.php中的內容加載到index.php容器中。
所以我的主要問題(問題2)是:我不知道如何編寫我的腳本或我的鏈接在我的PHP頁面,以便加載我的index.php文件中的內容,當我點擊時,我刷新和我後退前進。
在pushstate的常規行爲中,瀏覽器刷新時,onpopstate可以將頁面內容加載到其他頁面的容器中(從single.php加載內容到index.php的容器中)?
我希望有人能幫我解釋它是如何工作的。我很難理解它如何與鏈接一起工作。
對不起,我的英語,我是法國人...
您刪除了原來的問題,因此我看不到您的回覆是什麼。爲什麼類似的重寫規則? –
我使用這個類似的重寫規則,因爲沒有它,當我刷新瀏覽器時,它會進入single.php頁面並加載它。有了這個重寫規則,當我刷新瀏覽器然後加載內容fron single.php時,它保持在index.php上。沒有重寫規則,我可以有這種行爲嗎? – freaky
我不熟悉歷史API - 但我仍然認爲你不應該有這樣的路線。它應該始終指向一個文件... –