Angularjs HTML5 mode on doesnt work even after rew

2019-05-06 14:59发布

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条回答
小情绪 Triste *
2楼-- · 2019-05-06 15:53

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>
查看更多
登录 后发表回答