之前負(fù)責(zé)SEO的同事說絕對(duì)路徑好,一直抱著懷疑的態(tài)度(因?yàn)榻^對(duì)路徑對(duì)開發(fā)者來(lái)說實(shí)在太蛋疼了),今天寫一些有路徑的代碼時(shí),想到了查一下資料。
Should I Use Relative or Absolute URLs? - Whiteboard Friday
以上原文地址是英文內(nèi)容,大概整理了一下,中文總結(jié)(包含個(gè)人理解與經(jīng)驗(yàn))如下。
統(tǒng)一地址
關(guān)于絕對(duì)路徑,首先你得有一個(gè)統(tǒng)一的域名。
http://www.example.com/
http://example.com/
https://www.example.com/
https://example.com/
對(duì)用戶而言,以上四個(gè)地址訪問的都是同一個(gè)網(wǎng)站,因此他們是一樣的。
對(duì)Google而言,它們認(rèn)為這是四個(gè)不同的地址,因此它們是不一樣的。
Google is seeing the URL from all four versions of your website. They're going to try to figure out which URL is the real URL and just rank that one. The problem with that is you're basically leaving that decision up to Google when it's something that you could take control of for yourself.
Google會(huì)嘗試著分辨哪一個(gè)地址是最主要的,如果這四個(gè)地址不是指向同一個(gè)站點(diǎn),那么它可能就會(huì)錯(cuò)認(rèn)一個(gè)錯(cuò)誤的地址為官方的主站。此時(shí)你要做的就是填坑了,包括自己網(wǎng)站里的坑和Google的坑。
內(nèi)部鏈接
絕對(duì)路徑: <a title="to page">
相對(duì)路徑: <a href="/page" title="to page">
相對(duì)路徑的好處:
- 對(duì)開發(fā)者而言,相對(duì)路徑更簡(jiǎn)短高效,在部署代碼的時(shí)候也更方便些。
Some content management systems -- and SharePoint is a great example of this -- have a staging environment that's on its own domain. Instead of being example.com, it will be examplestaging.com.
絕對(duì)路徑的好處:
- 防止扒竊者輕松地把代碼給扒了。
如果內(nèi)部鏈接全是相對(duì)路徑,這其他人可以把所有代碼扒下來(lái),不用做任何修改直接部署到服務(wù)器上,那樣就有一個(gè)和你一模一樣的網(wǎng)站了。如果使用絕對(duì)路徑,至少扒竊者還得改路徑,如果沒改路徑,那么最后鏈接點(diǎn)擊了還是會(huì)跳轉(zhuǎn)到你的網(wǎng)站。 - 使用相對(duì)路徑的情況下,在上面的四種可能的域名中,Google認(rèn)為在四個(gè)版本的網(wǎng)站底下的相對(duì)路徑中的內(nèi)容是不一樣的。
例如:
在Google眼里,
https://example.com/
https://www.example.com/
上面兩個(gè)域名是不一樣的,
<a href="/page" title="to page">所鏈接的地址則分別為
https://example.com/page
https://www.example.com/page,
因此這兩個(gè)鏈接的內(nèi)容是不一樣的。
However, Google has gotten pretty good at figuring out what the real version of your website is.
Google能做好它該做的,而我們也應(yīng)該做好我們?cè)撟龅氖隆?/p>
- 搜索引擎抓取成本。
Google對(duì)網(wǎng)站抓取的深度和頻率是有一定限制的,這個(gè)限制就是SEO做得好不好的體現(xiàn)(SEO在此不詳談)。
如果使用相對(duì)路徑,在https://example.com/下,Google需要抓取一下相對(duì)路徑下的/page,在https://www.exaple.com/下需要抓取一次/page。如果使用的是絕對(duì)路徑<a title="to page">則Google只需要抓取一次這個(gè)鏈接。
Google如果需要花費(fèi)很大精力在抓取與分辨它倆的話,很快它就不愿意來(lái)光顧你的網(wǎng)站了。
修正路徑問題
- 服務(wù)端。
把一個(gè)域名定為主域名,其他多個(gè)域名解析(301跳轉(zhuǎn))到這個(gè)域名。
Google has certainly said that HTTPS is a little bit better than HTTP.
https是大勢(shì)所趨,帶不帶www就只是長(zhǎng)短問題了。
- 內(nèi)部鏈接。
把相對(duì)路徑改為絕對(duì)路徑,特別是header和footer。 Canonicalize it!
But having each page self-canonicalize will mitigate both the risk of duplicate content internally and some of the risk posed by scrappers, because when they scrape, if they are scraping your website and slapping it up somewhere else, those canonical tags will often stay in place, and that lets Google know this is not the real version of the website.上面這一段并沒有看很懂,大概意思是給頁(yè)面加一個(gè)
canonical tag
大概是長(zhǎng)這個(gè)樣子:
<link rel="canonical" />
關(guān)于canonical tab查了資料補(bǔ)充一下:
原文地址: Canonicalization
對(duì)Google而言,
https://www.example.com/page.html?param=1
https://www.example.com/page.html?param=2
以上兩個(gè)地址指向的是不一樣的地址,因此會(huì)抓取兩次,此時(shí)如果在page.html的<head>里面加入canonical tag,也就是
<link rel="canonical" />
那么瀏覽器就會(huì)知道只要抓取page.html這個(gè)頁(yè)面就行了,就會(huì)提高抓取的效率。
總結(jié)
- 統(tǒng)一域名
- 絕對(duì)路徑