Rails SEO 第二步: 關於 sitemap

紅寶鐵軌客
Join to follow...
Follow/Unfollow Writer: 紅寶鐵軌客
By following, you’ll receive notifications when this author publishes new articles.
Don't wait! Sign up to follow this writer.
WriterShelf is a privacy-oriented writing platform. Unleash the power of your voice. It's free!
Sign up. Join WriterShelf now! Already a member. Login to WriterShelf.
寫程式中、折磨中、享受中 ......
1.2K   0  
·
2017/04/24
·
3 mins read


有沒有辦法讓Google 關鍵字搜索找的到的第二篇:改 sitemap

Sitemap 基本上就是跟讓搜尋引擎說,怎麼去找網站中的資料,這可是最重要的一步,但是,用Rails來開發,很簡單。

先訂一個工作目標,網站Sitemap 要包含哪些:

基本上,一般都會有兩的部分:靜態的網頁與動態的網頁,如果還有分語言,我的建議是把它依語言版本分開,也就是,主要Sitemap 連到各不同的語言版Sitemap。

我研究了好幾個Sitemap gems,也考慮過自己寫,最後,我決定用:sitemap_generator gem,主要是,這Gem寫得很好,而且,這部分,跟未來網頁新功能開發與維護並不會有很大的關聯,這樣,使用Gem就好,不用花太多時間在這上面,自己開發有彈性,但是,相對遇到問題與debug的時間也會長。

在 gemfile 加入:

# sitemap generator
gem 'sitemap_generator'
 
$bundle install

$rake sitemap:install

再來就是要在robots.txt (config/robots.production.txt)中加上:

Sitemap: http://www.your_url.com/sitemap.xml.gz

再把你的Sitemap 產生碼寫到sitemap.rb,產生它,再:

$rake sitemap:refresh:no_ping

完成,哈哈哈哈,當然沒那麼簡單,但是基本上,就這樣。

以下是我的 Sitemap 產生碼的參考,我覺得,這是最簡單實用的介紹了。

# Set the host name for URL creation
SitemapGenerator::Sitemap.default_host = "https://your_url.com"
# 我有用到xxx_url helper,所以要是定網址
default_url_options[:host] = "https://www.your_url.com"

group(:filename => :english) do
 add '/?locale=en', :alternate => { :href => 'https://www.your_url.com, :lang => 'en'}, changefreq: 'daily', priority: 1.0
add '/help', :alternate => { :href => 'https://www.your_url.com/help?', :lang => 'en'}, changefreq: 'weekly'
 Blog.where(test1: true).where("test2 = 0 and language='en'").find_each do |blog|
  add blog_path(blog.friendly_id), :alternate => { :href => blog_url(blog.friendly_id), :lang => 'en'}, lastmod: blog.updated_at
 end

 Category.where("language='en'").where("count > 0").find_each do |c1|
  add list_path(cat_id: c1.id), :alternate => { :href => list_url(cat_id: c1.id), :lang => 'en'}, changefreq: 'daily', priority: 0.5
  c1.sub.find_each do |c2|
  add list_path(cat_id: c2.id), :alternate => { :href => list_url(cat_id: c2.id)", :lang => 'en'}, changefreq: 'daily', priority: 0.5
  end

 end

end

 group(:filename => :chinese) do
 跟上面一樣啦
end

end
 

development site 上測試一下,如果沒問題,這時就可以deploy到production site 上做測試了: 

$cd /apps/hello_app/current
$RACK_ENV=production bundle exec rake sitemap:refresh
 

如果一切順利,你就會看到類似以下的訊息:

In '/home/deploy/apps/xxx_app/releases/20170654327/public/':
+ english.xml.gz 43 links / 1.09 KB
+ chinese.xml.gz 43 links / 1.75 KB
+ sitemap.xml.gz 2 sitemaps / 247 Bytes
Sitemap stats: 86 links / 2 sitemaps / 0m00s

Pinging with URL 'https://www.your_url.com/sitemap.xml.gz':
Successful ping of Google
Successful ping of Bing 

做些收尾的工作:

  1. .gitignor: 註明不用上傳 developement 的測試 sitemap,不然會蓋掉production site sitemap
  2. 在Capistrano (如果你是用這,其他也差不多啦)加上deploy 完後,要重新產生Sitemap,
  3. crobtab -e 加上產生Sitemap的時間,我是每天上午七點。

in deploy.rb

desc 'rebuild sitemap'
task :build_sitemap do
# after deploy done, run sitemap rebuild and notice google bing
on roles(:app) do
execute <<-EOBLOCK
cd #{current_path}
RACK_ENV=production bundle exec rake sitemap:refresh
EOBLOCK
 end
end

after :finished, :build_sitemap
 
in crobtab -e
0 7 * * * /bin/bash -l -c 'cd /home/deploy/apps/xxx_app/current && RACK_ENV=production bundle exec rake sitemap:refresh'
 

恭喜,你已經完成第二步了!

關於行動網頁,如果你有,我的建議是要加寫 sitemap,畢竟,網址不同,但是,如果是RWD,我就不寫了,Google 沒那麼笨啦。

這時,你就要上 Google Search Console 去看看你的網頁上傳的紀錄與有沒有錯誤了,使用 Google Search console (Google web master)不會太難,但也不簡單,可以看到很多資訊,一定要花些時間把它搞懂,我就不在這寫了,畢竟,Google 也一天到晚改,我的經驗是上傳後,一天內,Google 就會更新,不算慢。

 

前一篇:Rails SEO 第一步: Robots.txt

下一篇:Rails SEO 第三步: 麻煩的 Metatag 及 OG


WriterShelf™ is a unique multiple pen name blogging and forum platform. Protect relationships and your privacy. Take your writing in new directions. ** Join WriterShelf**
WriterShelf™ is an open writing platform. The views, information and opinions in this article are those of the author.


Article info

This article is part of:
Categories:
Tags:
Date:
Published: 2017/04/24 - Updated: 2017/10/27
Total: 888 words


Share this article:
About the Author

很久以前就是個「寫程式的」,其實,什麼程式都不熟⋯⋯
就,這會一點點,那會一點點⋯⋯




Join the discussion now!
Don't wait! Sign up to join the discussion.
WriterShelf is a privacy-oriented writing platform. Unleash the power of your voice. It's free!
Sign up. Join WriterShelf now! Already a member. Login to WriterShelf.