AngularJS: can't get html5 mode urls with ui-route $state

前端 未结 2 1903
走了就别回头了
走了就别回头了 2020-12-02 21:22

I\'m using ui-router for state management but I think I\'m having trouble with my .htaccess rewrite rules. All my states worked when using /#/account style urls. Now I\'ve e

相关标签:
2条回答
  • 2020-12-02 21:58

    If working in a shared hosting that doesn't allow to edit Apache configuration you can set this in your .htaccess file to make $locationProvider.html5Mode(true) work (as @Shanimal pointed in @nfiniteloop answer):

    RewriteEngine on
    
    RewriteBase /path/to/app #change this!
    
    # Don't rewrite files or directories
    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]
    

    And don't forget to add <base href="/path/to/app/"> in the HTML <head>.

    0 讨论(0)
  • 2020-12-02 22:18

    This configuration has worked for many of our Apache users using html5mode and ui-router.

    <VirtualHost *:80>
        ServerName my-app
    
        DocumentRoot /path/to/app
    
        <Directory /path/to/app>
            RewriteEngine on
    
            # Don't rewrite files or directories
            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]
        </Directory>
    </VirtualHost>
    

    Also please make sure you are not a victim of <base href> bug. See the ui-router FAQ for more info.

    0 讨论(0)
提交回复
热议问题