Removing # from url of angularJS for SEO with back

2019-01-20 02:34发布

问题:

I know removing hash from AngularJS is pretty straightforward but the problem is that the backend is in Django.

So, the app works unless the page is refreshed using "F5".

so, http://127.0.0.1:8000/account works if the button is clicked but refreshing the page gives me Page not found as the server searches for it in urls.py file

Can someone please suggest me any fix for this ?

回答1:

Everything is right. When you refresh the page, firstly request gets processed on the server (and goes to django router). So server should know that it should return your angular page for this URL.

Let's assume that your page that contains Angular application lives on view called index. Then just point this url to it:

urlpatterns = [
    url(r'^account/$', index), 
]

or point all urls to your view (in case you don't need any other url's to be processed without angular):

//something like this, not really sure about the regex
urlpatterns = [
    url(r'^.*$', index), 
]

or something like

urlpatterns = [
    url(r'^/account/.*$', index), 
]

You're not alone with this issue: see this and this. So as you can see, it's not Django-specific trouble, but some general client-server workflow.



回答2:

Use Locationprovider instead of routeProvider and enable html5 to true. https://docs.angularjs.org/api/ng/provider/$locationProvider