刷新页面给出“页面未find”

我有一个应用程序,它使用单个ng视图和多个控制器和视图。 如果我浏览根目录,例如:www.domain.com,一切正常。 除了如果我按Ctrl + R(刷新),那么它给了我404错误页面找不到。 例如:在这个页面刷新给我错误。 http://domain.com/item/1/ 。

但是,如果我使用hashbang访问该页面,那么它的工作方式如下: http ://domain.com/#!/item/1/

我该如何解决这个问题? 我htacess是空的。 而且我使用支持html5 hashbang的chrome。

当浏览器调用http://example.com/#!/item/1/ ,它调用http://example.com/#!/item/1/的索引页面,然后JS通过分析标签来确定要显示的内容。

当浏览器调用http://example.com/item/1/ ,您的服务器正在尝试提供http://example.com/item/1/的索引页面,该页面无法find,因此会引发404错误。

要达到你想要的,你要么需要:

  • 创build一个重写规则来重写指向根索引页面的链接
  • 调整你的JS,以便它生成标签,而不是URL。 如果你正在使用AngularJS,那么用$locationProvider.html5Mode(false);closureshtml5模式$locationProvider.html5Mode(false); , 要么
  • http://example.com/item/1/中的索引页面redirect到http://example.com/#!/item/1/ – 但请注意,这将需要重复每个/ prettyPath /你克里特。

假设您使用的是Apache,并且索引文件是index.html,则尝试将下列内容添加到.htaccess文件中,以在尝试其他两种解决scheme之前创build重写规则。

 <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*) /index.html/#/$1 </IfModule> 

如果您使用的是纯粹的AngularJS / Ajax解决scheme,而没有服务器端逻辑,请将index.php更改为index.html(或index.htm,具体取决于您的根目录文件名)。

你需要在你的.htaccess中添加下面的脚本

 RewriteEngine On Options FollowSymLinks RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /#/$1 [L] 

这是URL重写(IIS)的.NET版本

 <system.webServer> <rewrite> <!--This directive was not converted because it is not supported by IIS: RewriteBase /.--> <rules> <rule name="Imported Rule 1" stopProcessing="true"> <match url="^index\.html" ignoreCase="false" /> <action type="None" /> </rule> <rule name="Imported Rule 2" stopProcessing="true"> <match url="." ignoreCase="false" /> <conditions> <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" /> </conditions> <action type="Rewrite" url="/index.html" /> </rule> </rules> </rewrite> </system.webServer> 

伟大的博客文章在这里… http://ericduran.io/2013/05/31/angular-html5Mode-with-yeoman/

“这主要是因为你的AngularJS路由不是实际的HTML页面,例如,如果你的angular度应用中有一个路由到/创build顺序,这个URL工作正常,如果你从你的应用程序链接到它,但如果用户试图直接进入该页面的服务器将返回一个404。

我无法评论,但也使用HTML modebase href="/"sudo a2enmod rewrite ,使用.htaccess重写。 我必须AllowOverride All在您的网站 /etc/apache2 apache.conf

  <ifModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !index RewriteCond %{REQUEST_URI} !.*\.(css js|html|png) RewriteRule (.*) index.html [L] </ifModule> 

使用这个.htaccess