除了主頁,我所有的 angularjs 網站都使用預渲染。抓取時,它會發回一個 404 頁面。我有理由相信這是我的 .htaccess 檔案中的這行代碼,RewriteRule ^(.*)$ http://service.prerender.io/https://%{HTTP_HOST}/$1 [P,L]但我不確定。
<IfModule mod_rewrite.c>
RewriteEngine On
# If requested resource exists as a file or directory
# (REQUEST_FILENAME is only relative in virtualhost context, so not usable)
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
# Go to it as is
RewriteRule ^ - [L]
# If non existent
# If path ends with / and is not just a single /, redirect to without the trailing /
RewriteCond %{REQUEST_URI} ^.*/$
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^(.*)/$ $1 [R,QSA,L]
# Handle Prerender.io
RequestHeader set X-Prerender-Token "notprovidingthiscode"
RewriteCond %{HTTP_USER_AGENT} baiduspider|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|quora\ link\ preview|showyoubot|outbrain|pinterest [NC,OR]
RewriteCond %{QUERY_STRING} _escaped_fragment_
# Proxy the request
RewriteRule ^(.*)$ http://service.prerender.io/https://%{HTTP_HOST}/$1 [P,L]
# If non existent
# Accept everything on index.html
RewriteRule ^ /index.html
uj5u.com熱心網友回復:
問題原來是 .htaccess 檔案在訪問 angularjs 應用程式的根目錄時服務于 example.com/index.html 而不僅僅是 example.com。這反過來又不能很好地與 ui-router 配合使用,因為 $stateProvider 不會在沒有明確說明的情況下在 url 末尾提供檔案名。訪問 example.com/index.html 確實導致我的頁面拋出 404 錯誤$urlRouterProvider.otherwise('404');
添加以下代碼解決了我的問題。
$urlRouterProvider.when('/index.html', '/');
這會將 example.com/index.html 重定向到 example.com,它指向 prerender.io 中的正確渲染。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/434256.html
