刷新页面显示“找不到页面" [英] Refreshing page gives "Page not found"
问题描述
我有一个应用程序,它使用单个 ng-view 和多个控制器和视图.如果我浏览根目录,例如:www.domain.com,一切正常.除了如果我按 Ctrl+R(刷新)然后它会给我 404 错误页面未找到.例如:在此页面上刷新会给我错误.http://domain.com/item/1/.
I have an app where it uses a single ng-view and multiple controllers and views. If I navigate through the root, eg: www.domain.com, everything works. Except that if I hit Ctrl+R (Refresh) then it gives me the 404 eror Page not Found. eg: refreshing on this page gives me error. http://domain.com/item/1/.
但是如果我使用 hashbang 访问页面,那么它就可以工作.例如:http://domain.com/#!/item/1/
But if I access the page using the hashbang then it works.eg: http://domain.com/#!/item/1/
我该如何解决这个问题?我的 htacess 是空的.我使用支持 html5 hashbang 的 chrome.
How shall I fix this problem? My htacess is empty. And Im using chrome which support html5 hashbang.
推荐答案
当浏览器调用 http://example.com/#!/item/1/
时,是在调用索引页http://example.com/
,然后您的 JS 通过分析主题标签来确定要显示的内容.
When the browser calls http://example.com/#!/item/1/
, it is calling the index page of http://example.com/
, your JS then determines what content to display by analysing the hashtag.
当浏览器调用 http://example.com/item/1/
时,您的服务器正在尝试提供 http://example.com/item/的索引页面1/
,它无法找到并因此引发 404 错误.
When the browser calls http://example.com/item/1/
, your server is attempting to serve the index page of http://example.com/item/1/
, which it cannot find and therefore throws a 404 error.
要实现您想要的,您要么需要:
To achieve what you want, you'll either need to:
- 创建重写规则以重写指向您的根索引页面的链接
- 调整您的 JS,使其生成主题标签而不是 URL.如果您使用的是 AngularJS,则使用
$locationProvider.html5Mode(false);
或 关闭 html5 模式 - 在
http://example.com/item/1/
中放置一个重定向到http://example.com/#!/item/1/
的索引页代码> - 但是请注意,这需要为每个/prettyPath/你 crete 重复.
- Create a rewrite rule to rewrite the links to your root index page
- adjust your JS so that it generates the hashtag instead of the URL. If you are using AngularJS then turn off html5 mode with
$locationProvider.html5Mode(false);
, or - put an index page in
http://example.com/item/1/
that redirects tohttp://example.com/#!/item/1/
- however note that this would need to be repeated for every /prettyPath/ you crete.
假设您正在使用 Apache 并且您的索引文件是 index.html,请尝试将以下内容添加到您的 .htaccess 文件中以创建重写规则,然后再尝试其他两种解决方案.
Assuming you are using Apache and your index file is index.html, try adding the following to your .htaccess file to create a rewrite rule before trying either of the other two solutions.
<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,具体取决于您的根索引文件名).
If you are using a pure AngularJS/Ajax solution without a server side logic, change index.php to index.html (or index.htm depending on your root index filename).
这篇关于刷新页面显示“找不到页面"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!