当浏览器调用时
http://example.com/#!/item/1/,它正在调用的索引页
http://example.com/,然后您的JS通过分析主题标签来确定要显示的内容。
当浏览器调用时
http://example.com/item/1/,您的服务器正在尝试提供的索引页
http://example.com/item/1/,但是找不到该索引页,因此会引发404错误。
要实现您想要的目标,您要么需要:
- 创建重写规则以重写指向根索引页面的链接
- 调整您的JS,以便它生成井号而不是URL。如果您使用的是AngularJS
$locationProvider.html5Mode(false);
,请使用或关闭html5模式 - 将索引页放在
http://example.com/item/1/
重定向到的页面http://example.com/#!/item/1/
-但是请注意,您创建的每个/ prettyPath /都需要重复执行此操作。
假设您正在使用Apache,并且索引文件为index.html,请在尝试其他两种解决方案之一之前,尝试将以下内容添加到.htaccess文件中以创建重写规则。
<IfModule mod_rewrite.c> RewriteEngine On Rewritebase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*) /index.html/#/$1 </IfModule>如果您使用的是不带服务器端逻辑的纯AngularJS /Ajax解决方案,请将index.php更改为index.html(或index.htm,具体取决于您的根索引文件名)。



