外部页面和Angular JS路由

时间:2015-01-03 01:16:44

标签: angularjs apache mod-rewrite

我在mysite.com/somedir/script.php处有一个PHP页面。

如果我的Angular视图中有<a>个标记指向此脚本,当我点击它们时,Angular的路由总会将我带回mysite.com/index.html

这是我的$routeProvider配置:

siteModule.config(function($routeProvider, $locationProvider) {
    $routeProvider
        .when('/', {
            templateUrl:'/views/blog.html',
            controller:'blogController'
        })
        .when('/blog',{
            templateUrl:'/views/blog.html',
            controller:'blogController'
        })
        .when('/work',{
            templateUrl:'/views/work.html',
            controller:'workController'
        });
    // For Pretty URLs
    $locationProvider.html5Mode(true);
});

我也在使用一些Apache mod_rewrite来处理我认为可能干扰的.htacess文件中的404s /页面刷新:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index
RewriteRule (.*) index.html [L]

前面提到的PHP页面是一个有效的(现有的)资源,所以我不确定为什么Angular或Apache会重定向/重写它。我在target="_blank"代码上使用了<a>,并在索引页的<base href="/">代码中设置了<head>

我只是希望能够正常访问我的脚本,而不是指向索引页面。

1 个答案:

答案 0 :(得分:0)

在javascript中,你仍然可以使用window.location.href来逃避路由器,所以在&#39;否则&#39;路由器的一部分,使用window.location.href = {route}而不是允许它使用默认索引。

请参阅此问题的其他答案:

Angular routes - redirecting to an external site?

Using $routeProvider to redirect to routes outside of Angular