从url angularjs中删除#符号后,Url刷新并重新加载

时间:2016-09-20 17:05:07

标签: angularjs url-rewriting pretty-urls

  

我的app.js

app.config(['$routeProvider','$locationProvider', function($routeProvider,$locationProvider) {
    $routeProvider.
    when('/', {
        templateUrl: 'interests.html',
        controller: 'InterestsController'
    })
    .when('/interests',{
        templateUrl: 'interests.html',
        controller: 'InterestsController'
    })
    .when('/subjects',{
        templateUrl: 'subjects.html',
        controller: 'SubjectsController'
    })
    .when('/prequalification',{
        templateUrl: 'prequalification.html',
        controller: 'PreQualificationController'
    })
    .when('/qualification',{
        templateUrl: 'qualification.html',
        controller: 'QualificationController'
    })
    .when('/personalinfo',{
        templateUrl: 'personalinfo.html',
        controller: 'PersonalInfoController'
    })
    .otherwise({
        redirectTo : '/interests'
    });
    if(window.history && window.history.pushState){
        $locationProvider.html5Mode(true);
    }
}]);
  

在index.html文件中。 index.html文件的路径:localhost:8080 / cf /。所有其他视图也驻留在index.html

<base href="/cf/">
  

.htaccess文件内容(.htaccess文件的路径:localhost:8080 / cf / .htaccess)

<ifModule mod_rewrite.c>
    RewriteEngine on

    # Don't rewrite files or directories
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]


    RewriteRule ^ cf/index.html [L]
</ifModule>
  

服务器上项目文件夹的路径是:localhost:8080 / cf。我在stackoverflow的其他帖子的.htaccess文件中有各种规则,但没有任何工作。甚至当我重新加载或刷新页面时,我得到404.在刷新和重新加载一切都工作正常。请帮忙!!!

2 个答案:

答案 0 :(得分:1)

第1步:在app config中添加

if(window.history && window.history.pushState){
    $locationProvider.html5Mode(true);
}

第2步:在index.html文件中添加

<base href="/your-app-name">

步骤3:在根目录上添加.htaccess文件

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^ index.html [L]

答案 1 :(得分:0)

使用

<base href="/cf">

在文件“head”部分。

希望这会有所帮助!!