10

在阅读了大量关于 Angular.js 路由的文章和 stackoverflow 问题后,当我手动刷新时,我仍然收到“未找到”错误。

脚步:

  1. 浏览到localhost--> 由于我的配置(如下),我被带到localhost/home. 视图和一切都加载正常。
  2. 在浏览器中点击刷新 --> 浏览器显示这个Not Found: the requested /home is not found on this server

这个问题可能最像刷新页面给出“找不到页面”

我的配置

// Routing configuration.
angular.module('myModule')
    .config(['$routeProvider', '$locationProvider', 
    function ($routeProvider, $locationProvider) {
        // Enable pushState in routes.
        $locationProvider.html5Mode(true);

        $routeProvider
            .when('/home', {
                templates: {
                    layout: '/views/home.html'
                },
                title: 'Welcome!'

            })
            .when('/launchpad', {
                templates: {
                    layout: '/views/layouts/default.html',
                    content: '/views/partials/profile.html'
                },
                title: "Launchpad"
            })
            .otherwise({
                redirectTo: '/home'
            });

    }
]);

我做过的其他事情:

  • 在我的index.html,我已经有了<base href="/">
  • 升级到角度 1.2.1

这是我尝试过的 htaccess 规则。没有工作。

刷新页面给出“找不到页面”

<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) #Add extra extensions needed.
    RewriteRule (.*) index.html [L]
</ifModule>

来自http://ericduran.io/2013/05/31/angular-html5Mode-with-yeoman/

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

4 回答 4

8

这个 .htaccess 设置对我来说效果很好

<IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !^/$
    RewriteRule (.*) /#!/$1 [NE,L,R=301]
</IfModule>
于 2014-08-26T19:22:58.403 回答
6

我不知道这是否是最佳解决方案,但我确实找到了有效的设置组合:

  • 包括hashPrefix('!')(见下文)
  • 未包含<base href="/">在我的 index.html 中
  • 根据这篇文章添加FallbackResource /index.html到我<Directory PATH_TO_WWW_FILES>的服务器.conf文件中的部分。设置后,本地设置似乎无关紧要。mod_rewrite
// Routing configuration.
angular.module('myModule')
    .config(['$routeProvider', '$locationProvider', 
    function ($routeProvider, $locationProvider) {
        // Enable pushState in routes.
        $locationProvider.html5Mode(true).hashPrefix('!');

        $routeProvider
            .when('/home', {
                templates: {
                    layout: '/views/home.html'
                },
                title: 'Welcome!'

            })
            .when('/launchpad', {
                templates: {
                    layout: '/views/layouts/default.html',
                    content: '/views/partials/profile.html'
                },
                title: "Launchpad"
            })
            .otherwise({
                redirectTo: '/home'
            });

    }
]);
于 2013-12-11T12:10:26.840 回答
1

对于仍然面临此错误的人,无论是否使用 SSL:

确保你 Allowoverride 在你的 apacheconfig 例如

<Directory "/var/www/mysite/public_html">
    AllowOverride All
</Directory>

对于这两个端口,您是否使用 SSL

于 2016-04-01T14:05:52.273 回答
1

我无法发表评论,但除了使用HTML mode, base href="/", sudo a2enmod rewrite, 使用.htaccess重写。从 Unispaw ,我必须AllowOverride All在您网站的可用网站/etc/apache2 apache.conf

于 2017-06-19T12:29:20.157 回答