获取404页的刷新在AngularJS网站? [英] Getting 404 page on refresh on AngularJS site?

查看:255
本文介绍了获取404页的刷新在AngularJS网站?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个所服务的404页,每当我:

I am getting a served the 404 page whenever I :

1)刷新我的Angularjs网站的任何页面的(除了根页)

1.) refresh any page on my Angularjs site (besides the root page)

2。)点击返回从404页到根页面去的(根页将是404)

2.) click Back to go from a 404 page to the root page (the root page will be a 404)

下面是我的的.config() code

Here is my .config() code

'use strict';

angular.module('mmApp', ['ngResponsiveImages'])
  .config(function ($locationProvider, $routeProvider) {
    $locationProvider.html5Mode(true);
    $locationProvider.hashPrefix = '!';
    $routeProvider
      .when('/', {
        templateUrl: '/views/design.html',
        controller: 'MainCtrl'
      })
      .when('/about', {
        templateUrl: '/views/about.html',
        controller: 'MainCtrl'
      })
      .when('/contact', {
        templateUrl: '/views/contact.html',
        controller: 'MainCtrl'
      })     
      .otherwise({
        redirectTo: '/'
      });
  });

我也碰到过这种类似的问题,但不太了解的答案,或如何实现它们。

I have come across this similar question but do not quite understand the answers or how to implement them.

<一个href="http://stackoverflow.com/questions/15250090/angularjs-why-when-changing-url-address-routeprovider-doesnt-seem-to-work-an">AngularJS - 为什么要改变URL地址时,$ routeProvider似乎没有工作,我得到一个404错误

接受的答案说要配置Apache来重新配置所有路径的根源。我的网站是在Github页面。我在我的 GH-页回购一个的.htaccess 文件,但我不知道如何配置它全部送路径到根

The accepted answer says to setup Apache to reconfigure all paths to the root. My site is on Github Pages. I have a .htaccess file in my gh-pages repo but I have no idea how to configure it to send all paths to the root.

回答说另一件事是使用考虑&LT;基地&GT; 像元素&LT;基地的HREF =//&GT; ,但我会在哪里放呢?在我的 index.html的?如果是这样,在该文件的顶部或底部?另外,我会刚刚离开的href /

The answer says another thing to consider is using the <base> element like <base href="/" /> but where would I place that? In my index.html? If so at the top or bottom of that file? Also, would I just leave the href value to /?

推荐答案

我碰到这里的解决方案: https://开头coderwall.com / P / kfomwa

I came across a solution here: https://coderwall.com/p/kfomwa

下面是短期岗位:

angularjs提供html5Mode,这使得井号标签的您的应用程序使用pushstate为基础的URL来代替。然而,这需要服务器端的支持,因为生成的URL需要渲染,以及适当的。

angularjs provides html5Mode, which makes your app use pushstate-based URL instead of hashtags. However this requires server side support, since the generated urls need to be rendered properly as well.

这其实正常工作与GitHub上页自定义404页,但它是仅适用于自定义域启用网页。

This actually works fine with github pages' custom 404 pages, though it's only available for custom domain enabled pages.

首先,复制里面的一切index.html来404.html ,然后,将其添加到您的应用程序:

First, copy everything inside index.html to 404.html. Then, add this to your app:

 angular.module('app', []).config(function($locationProvider) {
      $locationProvider.html5Mode(true);
    });

请注意,如果你是对角1.1.5,请确保您设置的html5Mode才能正常工作。

Note that if you are on angular 1.1.5, make sure you set for html5Mode to work properly.

以下这些建议我能得到我的网站正常工作。现在,当我打刷新,页面加载正常。

Following these suggestions I was able to get my site working properly. Now when I hit Reload, the page loads properly.

这篇关于获取404页的刷新在AngularJS网站?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆