React+React Router v6站点首页被索引为自定义404(已接Prerender.io)
首页被搜索引擎识别为404的排查与修复方案
核心问题分析
搜索引擎爬虫通过Prerender.io抓取首页时,服务器返回了404状态码,但直接访问时返回正常。问题大概率出在.htaccess规则顺序或Prerender的配置上。
1. 调整ErrorDocument的位置
当前ErrorDocument 404放在了<IfModule mod_rewrite.c>内部,可能干扰Prerender的代理请求。把它移到模块外部:
<IfModule mod_rewrite.c> # 所有Rewrite规则保持不变... </IfModule> # 把404配置移到这里 ErrorDocument 404 https://dental-home.ru/error
2. 验证Prerender返回的状态码
用curl模拟爬虫请求,检查首页的返回状态:
curl -A "googlebot" -I https://dental-home.ru/
正常应该显示HTTP/1.1 200 OK,如果返回404:
- 检查Prerender令牌是否正确
- 确认Prerender服务能正常访问你的网站
- 排查首页是否有JS错误导致Prerender无法完成渲染
3. 确认路由逻辑无异常
虽然直接访问正常,但要确保Home组件没有在渲染时触发重定向到错误页面的逻辑;通配符路由path="*"仅会在未匹配到任何路由时触发,不会影响首页。
4. 检查301重定向规则
现有301规则都是针对特定旧路径,不会影响首页,但可以确认没有误写规则导致首页被意外重定向。
用户原始配置信息
React Router v6路由配置
<Router> <ScrollToTop></ScrollToTop> <Routes> <Route path="/" element={<Home />} /> <Route path="/children-dental" element={<ChildDental />}></Route> <Route path="/prices" element={<PricesPage />}></Route> <Route path="/privacy" element={<Privacy />}></Route> <Route path="/reviews" element={<ReviewsPage />}></Route> <Route path="/works" element={<WorksPage />}></Route> <Route path="/doctors" element={<DoctorsPage />}></Route> <Route path="/clinics" element={<ClinicsPage />}></Route> <Route path="/contacts" element={<ContactsPage />}></Route> <Route path="/services/whitening" element={<Whitening />}></Route> <Route path="/services/hygiene" element={<Hygiene />}></Route> <Route path="/services/bite-correction" element={<BiteCorrection />} ></Route> <Route path="/services/teeth-healing" element={<TeethHealing />}></Route> <Route path="/services/prosthetics" element={<ProstheticsPage />}></Route> <Route path="/services/vinirs" element={<VinirsPage />}></Route> <Route path="/services/surgery" element={<Surgery />}></Route> <Route path="/services/diagnostic" element={<DiagnosticPage />}></Route> <Route path="/services/child-bite-correction" element={<ChildBiteCorrection />} ></Route> <Route path="/services/child-diagnostic" element={<ChildDiagnostic />} ></Route> <Route path="/services/child-surgery" element={<ChildSurgery />}></Route> <Route path="/services/child-teeth" element={<ChildTeeth />}></Route> <Route path="/services/child-hygiene" element={<ChildHygiene />}></Route> <Route path="/services/child-sleep" element={<ChildSleep />}></Route> <Route path="/error" element={<Error />}></Route> <Route path="*" element={<Error />} /> </Routes> </Router>
.htaccess配置
<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / Redirect 301 /paradontologiya https://dental-home.ru/services/diagnostic Redirect 301 /uslugi https://dental-home.ru/#services Redirect 301 /diagnostika https://dental-home.ru/services/diagnostic Redirect 301 /protezirovanie https://dental-home.ru/services/prosthetics Redirect 301 /implantatsiya https://dental-home.ru/services/prosthetics Redirect 301 /gigiena https://dental-home.ru/services/hygiene Redirect 301 /lechenie https://dental-home.ru/services/vinirs Redirect 301 /anasteziologiya https://dental-home.ru/services/diagnostic # 重定向www到非www RewriteCond %{HTTP_HOST} ^www\.(.*)$ RewriteRule ^(.*)$ https://%1/$1 [L,R=301] # Prerender.io配置 <IfModule mod_headers.c> # 设置Prerender令牌 RequestHeader set X-Prerender-Token "MY_TOKEN" </IfModule> RewriteCond %{HTTP_USER_AGENT} googlebot|bingbot|yandex|baiduspider|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|quora\ link\ preview|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Validator [NC,OR] RewriteCond %{QUERY_STRING} _escaped_fragment_ # 如果是HTML请求(排除指定文件类型),将请求代理到Prerender RewriteRule ^(?!.*?(\.js|\.css|\.xml|\.less|\.png|\.jpg|\.jpeg|\.gif|\.pdf|\.doc|\.txt|\.ico|\.rss|\.zip|\.mp3|\.rar|\.exe|\.wmv|\.doc|\.avi|\.ppt|\.mpg|\.mpeg|\.tif|\.wav|\.mov|\.psd|\.ai|\.xls|\.mp4|\.m4a|\.swf|\.dat|\.dmg|\.iso|\.flv|\.m4v|\.torrent|\.ttf|\.woff|\.svg|\.eot))(.*) http://service.prerender.io/https://dental-home.ru/$2 [P,L] # 现有规则 RewriteRule ^index\.html$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-l RewriteRule . /index.html [L] ErrorDocument 404 https://dental-home.ru/error </IfModule>
内容的提问来源于stack exchange,提问作者Ruslan Makhmatov
相关产品推荐
相关产品推荐

