Angularjs HTML5 mode on doesnt work even after rewriting URLs on server side

浪子不回头ぞ 提交于 2019-12-07 09:03:00

问题


In my Angularjs application the '#' was present initially in the URL. So I set the HTML5 mode for the app to true and also made the necessary changes all over the application code for URLs. PFB my code for that.

app.config(function($locationProvider){ $locationProvider.html5Mode({
    enabled: true,
    requireBase: false
});
});

Now as per this link I made the necessary changes in my configuration file. Those changes are as follows:

Options FollowSymLinks
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /$1 [L]

The Problem:

The application still won't work and it still doesn't refresh the URL i.e. I still get the 404: Page not found error.

Please help me regarding this.


回答1:


Well, setting requireBase: true and inserting <base href="/">(or what ever else, depending on the env) in the header, and using this settings

    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]

works for me in every places that i ever used it. Your DirectoryIndex must be set to index.html or the name of the file you are using and this is very important, your server must always send your initial file.

The following is an example, of what i'm using right now, but it is in my pc, no public hosting.

<VirtualHost 127.0.0.1:80>
    ServerName www.myproject.localhost
    DocumentRoot "d:/Work/myproject/"
    DirectoryIndex index.html
    <Directory "d:/Work/myproject/">
        Options +FollowSymlinks
        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]

        AllowOverride All
        Allow from All
        Require all granted
    </Directory>
</VirtualHost>


来源:https://stackoverflow.com/questions/38186723/angularjs-html5-mode-on-doesnt-work-even-after-rewriting-urls-on-server-side

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