Skip to main content

[Thủ Thuật WP] Sửa lỗi 404 khi tạo sitemap với Wordpess Yoast SEO- Rank Math

Bạn đang dùng plugin Wordpress Yoast SEO hoặc Rank Math để tối ưu SEO cho Wordpess và dùng để tạo sitemap cho website/blog?

Và khi submit sitemap lên Google Webmasters Tool thì có thông báo về lỗi 404 Not Found. Và bạn thử kiểm tra sitemap của mình theo đường dẫn www.example.com/sitemap.xml hoặc www.example.com/sitemap_index.xml thì bị trả về lỗi 404 Not Found (lỗi không tìm thấy).

Dưới đây là cách khắc phục lỗi

Cách khắc phục lỗi 404 của sitemap Yoast SEO hoặc Rank Math

Nguyên nhân bị lỗi 404 do cấu trúc đường dẫn tĩnh (Permalinks)

Nếu bị lỗi do cáu trúc đường dẫn, bạn truy cập vào trang quản trị Admin -> Cài đặt (Settings) -> Đường dẫn tĩnh (Permalinks) sau đó bấm vào Lưu (Save) mà không cần phải thay đổi bất cứ điều gì.

Image
Nguyên nhân bị lỗi 404 do cấu trúc đường dẫn tĩnh (Permalinks)

 Sau đó trở lại  www.example.com/sitemap_index.xml  để kiểm tra. 

Nguyên nhân bị lỗi 404 do chuyên mục, thẻ tag, nội dung bị loại trừ khỏi sitemap.

Lỗi này bạn sẽ gặp phải khi truy cập vào 1 trong các đường dẫn như /category-sitemap.xml, /post-sitemap.xml, /post_tag-sitemap.xml,….thì bị báo lỗi 404 not found.

Điều này có thể do khi thiết lập sitemap trong Plugin bạn đã loại trừ nó ra khỏi sitemap nên nó sẽ bị lỗi.

Bạn có thể khắc phục bằng cách truy cập trang quản trị Admin -> SEO -> XML Sitemaps và xem các mục đó có bị loại trừ khỏi sitemap không.

Nguyên nhân bị lỗi 404 tương ứng với hosting/sever

Để giải quyết được vấn đề này bạn cần biết hosting/server bạn đang chạy web sử dụng Apache hay sử dụng Nginx.

1. Dành cho Plugin Yoast SEO

Dành cho Apache

Nếu bạn đang sử dụng Apache thì chỉ cần thêm đoạn code sau vào file .htaccess và lưu lại.

Lưu ý: Trước khi làm bạn nên backup lại file .htaccess đề phòng xảy ra lỗi.

# Yoast SEO - XML Sitemap Rewrite Fix
RewriteEngine On
RewriteBase /
RewriteRule ^sitemap_index.xml$ /index.php?sitemap=1 [L]
RewriteRule ^locations.kml$ /index.php?sitemap=wpseo_local_kml [L]
RewriteRule ^geo_sitemap.xml$ /index.php?sitemap=geo [L]
RewriteRule ^([^/]+?)-sitemap([0-9]+)?.xml$ /index.php?sitemap=$1&sitemap_n=$2 [L]
RewriteRule ^([a-z]+)?-?sitemap.xsl$ /index.php?yoast-sitemap-xsl=$1 [L]
# END Yoast SEO - XML Sitemap Rewrite Fix

Dành cho Nginx

Nếu bạn đang dùng Nginx mà sitemap bị lỗi 404 thì mở file cấu hình domain trong Nginx theo đường dẫn /home/example.com/public_html/nginx.conf  và thêm vào đoạn code sau:

#Yoast SEO Sitemaps
location ~ ([^/]*)sitemap(.*).x(m|s)l$ {
  ## this rewrites sitemap.xml to /sitemap_index.xml
  rewrite ^/sitemap.xml$ /sitemap_index.xml permanent;
  ## this makes the XML sitemaps work
  rewrite ^/([a-z]+)?-?sitemap.xsl$ /index.php?yoast-sitemap-xsl=$1 last;
  rewrite ^/sitemap_index.xml$ /index.php?sitemap=1 last;
  rewrite ^/([^/]+?)-sitemap([0-9]+)?.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
  ## The following lines are optional for the premium extensions
  ## News SEO
  rewrite ^/news-sitemap.xml$ /index.php?sitemap=wpseo_news last;
  ## Local SEO
  rewrite ^/locations.kml$ /index.php?sitemap=wpseo_local_kml last;
  rewrite ^/geo-sitemap.xml$ /index.php?sitemap=wpseo_local last;
  ## Video SEO
  rewrite ^/video-sitemap.xsl$ /index.php?yoast-sitemap-xsl=video last;
}

Nếu bạn sử dụng multiple site hoặc wordpress trong subfolder thì sử dụng

location ~ ([^/]*)sitemap(.*).x(m|s)l$ { 
  ## this makes the XML sitemaps work 
  rewrite ^.*/([a-z]+)?-?sitemap.xsl$ /index.php?yoast-sitemap-xsl=$1 last; 
  rewrite ^.*sitemap_index.xml$ /index.php?sitemap=1 last; 
  rewrite ^.*/([^/]+?)-sitemap([0-9]+)?.xml$ /index.php?sitemap=$1&sitemap_n=$2 last; 
  # Rules for yoast    sitemap with wp|wpsubdir|wpsubdomain 
  rewrite ^.*sitemap_index\.xml$ /index.php?sitemap=1 last; 
  rewrite ^.*/([^/]+?)-sitemap([0-9]+)?\.xml$ /index.php?sitemap=$1&sitemap_n=$2 last; 
  ## The following lines are optional for the premium extensions 
  ## News SEO 
  rewrite ^.*news-sitemap.xml$ /index.php?sitemap=wpseo_news last; 
  ## Local SEO 
  rewrite ^.*locations.kml$ /index.php?sitemap=wpseo_local_kml last; 
  rewrite ^.*geo-sitemap.xml$ /index.php?sitemap=wpseo_local last; 
  ## Video SEO 
  rewrite ^.*video-sitemap.xsl$ /index.php?yoast-sitemap-xsl=video last; 
}

Sau đó reset Server để kiểm tra lại.

2. Dành cho Plugin Rank Math

Dành cho Apache

# START of Rank Math Sitemap Rewrite Rules
RewriteEngine On
RewriteBase /
RewriteRule ^sitemap_index.xml$ /index.php?sitemap=1 [L]
RewriteRule ^([^/]+?)-sitemap([0-9]+)?.xml$ /index.php?sitemap=$1&sitemap_n=$2 [L]
RewriteRule ^([a-z]+)?-?sitemap.xsl$ /index.php?xsl=$1 [L]
# END of Rank Math Sitemap Rewrite Rules

Dành cho Nginx

# START Nginx Rewrites for Rank Math Sitemaps
rewrite ^/sitemap_index.xml$ /index.php?sitemap=1 last;
rewrite ^/([^/]+?)-sitemap([0-9]+)?.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
# END Nginx Rewrites for Rank Math Sitemaps