AngularJS w/Prerender 主页出现404错误

AngularJS w/Prerender 404 error on home page

我的所有 angularjs 站点都可以使用预呈现,但主页除外。抓取时,它会发回 404 页面。我有理由相信这是我的 .htaccess 文件中的这行代码,RewriteRule ^(.*)$ http://service.prerender.io/https://%{HTTP_HOST}/ [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 ^(.*)/$  [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}/ [P,L]

# If non existent
# Accept everything on index.html
RewriteRule ^ /index.html

问题原来是 .htaccess 文件正在服务示例。com/index.html 而不是 example.com 在访问 angularjs 的根目录时应用程序。这反过来又不能很好地与 ui-router 一起使用,因为 $stateProvider 不会在不明确的情况下在 url 末尾提供文件名。访问示例。com/index.html 确实导致我的页面抛出 404 错误 $urlRouterProvider.otherwise('404');

添加以下代码解决了我的问题。

$urlRouterProvider.when('/index.html', '/');

这会将示例。com/index.html 重定向到 example.com,它指向 prerender.io.

中的正确呈现