如何避免404时使用历史API HTML5

How avoid 404 when using history API HTML5

本文关键字:历史 API HTML5 何避免 404时      更新时间:2023-09-26

在SennaJS和EmberJS中都使用历史API。当URL粘贴在地址栏中时,这些库如何避免404。

您必须修改您的Web服务器并启用mod_rewrite。然后服务器将接收请求并将其转发到索引文件,ember和SennaJS将按照您的期望解析URL。

对于Apache,你需要启用mod重写和编辑你的vhost:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index'.html$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.html [L]
 </IfModule>

和nginx:

rewrite ^(.+)$ /index.html last;

请参阅:http://readystate4.com/2012/05/17/nginx-and-apache-rewrite-to-support-html5-pushstate/