最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular route with html5Mode giving 'Not found' page after reload - Stack Overflow

programmeradmin4浏览0评论

I made some Angular routes as shown in the code bellow.

app.config(function($routeProvider, $locationProvider, $provide) {
    $routeProvider
    .when('/', {
         templateUrl: 'home.html',
         controller: 'AppCtrl'
    });
    .when('/portfolio', {
        templateUrl: 'portfolio.html',
        controller: 'AppCtrl'
    })
    $provide.decorator('$sniffer', function($delegate) {
         $delegate.history = historyCompatCheck();
         return $delegate;
    });
    $locationProvider.html5Mode(true);
});

This works fine, after i set the base href to be "/" it accepted an anchor with the href of "/portfolio", but when i go to "" or try to reload the page when i'm on the portfolio route it will give me an server error. Can i do something about this?

Thanks in advance.

I made some Angular routes as shown in the code bellow.

app.config(function($routeProvider, $locationProvider, $provide) {
    $routeProvider
    .when('/', {
         templateUrl: 'home.html',
         controller: 'AppCtrl'
    });
    .when('/portfolio', {
        templateUrl: 'portfolio.html',
        controller: 'AppCtrl'
    })
    $provide.decorator('$sniffer', function($delegate) {
         $delegate.history = historyCompatCheck();
         return $delegate;
    });
    $locationProvider.html5Mode(true);
});

This works fine, after i set the base href to be "/" it accepted an anchor with the href of "/portfolio", but when i go to "http://url./portfiolo" or try to reload the page when i'm on the portfolio route it will give me an server error. Can i do something about this?

Thanks in advance.

Share Improve this question edited Jun 21, 2017 at 0:10 laser 1,37613 silver badges14 bronze badges asked Aug 26, 2013 at 20:53 jvakuilerjvakuiler 5281 gold badge6 silver badges17 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

I solved it myself.. you always need to make a .htaccess file in the root of your project. Containing the following:

 <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
 </IfModule>
#BEGIN

Options +FollowSymLinks

<ifModule mod_rewrite.c>
    RewriteEngine on

    # Don't rewrite files or directories
     RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} -s [OR]
   RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^.*$ - [NC,L]

   RewriteRule ^(.*) your root folder/index.php [NC,L]

 </ifModule>
   #END

its working 100% <base href="/"></base> also add to your index.html code in <head>section

发布评论

评论列表(0)

  1. 暂无评论