AngularJS page not working when refresh/share a link

心已入冬 提交于 2020-01-16 00:52:14

问题


I have a webpage and it's working just fine. The problem starts when i need to refresh the page or try to share a link with someone (or even open it in a new tab).

When i try to do it, the page doesn't work. It loads only the header, footer, etc. But not the part inside my ng-view and also no css class, etc...

Also, I'm using html5 mode in AngularJS with this code:

$locationProvider.html5Mode({
    enabled:true
});

I tried to use a .htaccess to reload the page and it's working, but only for the first link. For example site.com/News is loading ok, but if I try to refresh/share the page site.com/News/4 (which goes to an individual news page) then it won't work anymore.

This is the code i used in my .htaccess file

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)       /index.html
</IfModule>

回答1:


Try this:

RewriteEngine On  
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
RewriteRule ^ /index.html


来源:https://stackoverflow.com/questions/31340154/angularjs-page-not-working-when-refresh-share-a-link

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!