【问题标题】:ngRoute adds trailing slash to some url but not allngRoute 将斜杠添加到某些 url 但不是全部
【发布时间】:2015-09-09 03:00:57
【问题描述】:

当用户转到 localhost:8888localhost:8888/ 时,我的 Angular 应用程序将 url 更改为 http://localhost:8888/#!/

这是我在主页中的 Angular 应用:

app.config(['$locationProvider', '$routeProvider',
    function($locationProvider, $routeProvider) {
        $locationProvider.hashPrefix('!');

        $routeProvider
            .when('/', {
                redirectTo: ((window.status === 'signup') ? '/signup' : '/signin')
            })
            .when("/signin", {
                templateUrl: "/public/user/signin.html",
                controller: "signinController"
            })
            .when('/signup', {
                templateUrl: "/public/user/signup.html",
                controller: "signupController"
            })
            .otherwise({
                redirectTo: '/'
            });
    }
])

如果用户转到http://localhost:8888/auth,角度重定向到http://localhost:8888/auth#!/signin

仅当用户转到 http://localhost:8888/auth/ 时,角度重定向到 http://localhost:8888/auth/#!/signin

app.config(['$locationProvider', '$routeProvider',
    function($locationProvider, $routeProvider) {
        $locationProvider.hashPrefix('!');

        $routeProvider
            .when('/', {
                redirectTo: ((window.status === 'signup') ? '/signup' : '/signin')
            })
            .when("/signin", {
                templateUrl: "/public/user/signin.html",
                controller: "signinController"
            })
            .when('/signup', {
                templateUrl: "/public/user/signup.html",
                controller: "signupController"
            })
            .otherwise({
                redirectTo: '/'
            });
    }
])

angular官网和大部分书籍推荐使用xxx/#!/xxx格式。我怎样才能做到这一点?

编辑:我知道我可以从服务器端添加斜杠。但用户可以直接输入xxx.com/auth。 为什么xxx.com 工作但xxx.com/auth 不工作?

Edit2:教程示例项目适用于所有 url。我们有几乎相同的实现

http://angular.github.io/angular-phonecat/step-8/app/#/phones

尝试输入http://angular.github.io/angular-phonecat/step-8/app(不带斜杠

教程链接:https://docs.angularjs.org/tutorial/step_07

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    最好的方法是从客户端而不是服务器附加它,因为用户最有可能在没有反斜杠的情况下输入 url。不应该有太多的开销,因为无论如何您都必须修改角度哈希片段。

    试试这个:

    angular.element(document).ready(function() {
        window.location.hash = '#!/' //use ! for google crawlers ajax support
        if (window.location.href.substr(-1) != '/' window.location.href += '/'
    })
    

    【讨论】:

    • 啊,它有效。但是有一些延迟,尤其是第一次加载
    【解决方案2】:

    这是您应该通过添加从 /auth/auth/ 的 301 重定向在后端解决的问题。

    Nginx:

    rewrite ^/auth$ /auth/ permanent;
    

    阿帕奇:

    Redirect "/auth" "/auth/" [R=301]
    

    【讨论】:

    • 我可以用角码修复它吗?因为用户最有可能输入www.xxx.com/auth
    • 您也可以使用 if (location.pathname === '/auth') { location.replace('/auth/'); } 使用 JavaScript 进行重定向,但最好在您的服务器上解决此问题,因为这样会更快。
    • 你的意思是 $location.replace?或 $window.location.replace()
    • $window.location.replace。但同样,在您的服务器上执行此操作会更快。用户会注意到 javascript 重定向的延迟。
    • 为什么localhost:8888 可以工作,而localhost:8888/auth 不行?
    猜你喜欢
    • 2012-03-31
    • 2013-06-13
    • 1970-01-01
    • 2016-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-26
    • 1970-01-01
    相关资源
    最近更新 更多