在AngularJS网站上无法在没有#的情况下进行路由

时间:2016-02-29 09:02:54

标签: angularjs .htaccess url-rewriting

我的.htaccess看起来像这样:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^thesite\.com\.cp\-45\.webhostbox\.net$ [OR]
RewriteCond %{HTTP_HOST} ^www\.thesite\.com\.cp\-45\.webhostbox\.net$
RewriteRule ^/?$ "http\:\/\/www\.thesite\.com\/" [R=301,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Rewrite everything else to index.html to allow html5 state links
RewriteRule ^ index.html [L]

我的基本网址在index.html中设置为root:

<base href="/">

我的AngularJS应用程序配置中还有以下代码行:

$locationProvider.html5Mode(true);

尽管如此,当我尝试直接访问我网站上没有URL中的#符号的任何页面时,我无法摆脱404。我可以关注链接,但一旦在页面上,刷新它就会返回404.我的重写中是否有遗漏的内容?

为了防止它有用,我的Angular app配置看起来像这样(下面的部分代码):

// create the module and name it asApp
var asApp = angular.module('asApp', ['ngRoute']);

// configure our routes
asApp.config(['$routeProvider', '$httpProvider', '$locationProvider', function($routeProvider, $httpProvider, $locationProvider) {
    $routeProvider

    // route for the home page
    .when('/', {
        title: 'Learn Spanish with thesite | Unconventional tricks for the crazy learner',
        templateUrl : 'pages/thesitehome.html?v=1.1',
        controller  : 'mainController'
    })

    // route for the about page
    .when('/about', {
        title: 'About thesite and its penchant for cunning Spanish learning tricks',
        templateUrl : 'pages/aboutus.html?v=2',
        controller  : 'mainController'
    })

    // route for dictionary term
    .when('/dictionary/:word2lookup', {
        templateUrl : 'pages/dictionary.html?v=1',
        controller  : 'dictController'
    })

    // route for dictionary
    .when('/dictionary', {
        title: 'The thesite dictionary – Look up Spanish or English words in a jiffy',
        templateUrl : 'pages/dictionary.html?v=1',
        controller  : 'mainController'
    })

    // route for the 404 page not found error
    .when('/notfound', {
        templateUrl : 'pages/404.html',
        controller  : 'mainController'
    })

    // route otherwise
    .otherwise({
        controller: 'mainController',
        templateUrl: 'pages/404.html'
    });
    $locationProvider.html5Mode(true);
}]);

任何朝着正确方向的推动都将受到高度赞赏!哦,有问题的网站是现场的,以防万一你想看看并亲眼看到问题。

1 个答案:

答案 0 :(得分:0)

尝试以下方式。

https://scotch.io/quick-tips/pretty-urls-in-angularjs-removing-the-hashtag

<!doctype html>
<html>
<head>
    <meta charset="utf-8">

    <base href="/">
</head>