Apache rewrite rules not being applied for angularjs

后端 未结 4 892
走了就别回头了
走了就别回头了 2020-12-03 06:10

I am trying to setup apache 2.2 with AngularJS with an almost exact structure as the one from the following closed question.

rewrite rules for apache 2 to use with

相关标签:
4条回答
  • 2020-12-03 06:47

    This works for me:

    <ifModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_URI} !index
        RewriteRule (.*) index.html [L]
    </ifModule>
    
    0 讨论(0)
  • 2020-12-03 06:55

    Don't know if you are still having this problem, but I was having the same symptoms with AngularJS and Apache 2.

    My problem was actually that I was using IE and it was automatically coming up in compatability mode. I used the line below in my HTML and it worked.

    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    

    Described further here: < What does <meta http-equiv="X-UA-Compatible" content="IE=edge"> do? >.

    0 讨论(0)
  • 2020-12-03 07:01

    I had this same issue. But I'm ignorant in mod_rewrite so had to Google a lot. I found the solution in this email:

    https://groups.google.com/d/msg/angular/GmNiNVyvonk/mmffPbIcnRoJ

    So I think your .htaccess should look as follows:

    Options +FollowSymLinks
    IndexIgnore */*
    
    RewriteEngine on
    
    # if a directory or a file exists, use it directly
    RewriteCond %{REQUEST_FILENAME} -s [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteCond %{REQUEST_URI} !/api
    RewriteRule ^.*$ - [NC,L]
    
    # otherwise forward it to index.html 
    RewriteRule ^app/(.*) /app/#/$1 [NC,L]
    

    Notice the (.*) and the #/$1

    Note: you MUST use absolute paths in your includes CSS, JS, etc. if not you are going to get the error:

    resource interpreted as script but transferred with mime type text/html

    0 讨论(0)
  • 2020-12-03 07:11

    This is now much easier in Apache 2.2.16+ using the FallbackResource directive.

    FallbackResource /app/index.html
    

    http://httpd.apache.org/docs/2.2/mod/mod_dir.html#fallbackresource

    Depending on how you're forwarding access to your API you may need to also leverage the enclosure to disable the fallback resource on specifically on API requests (2.2.24+).

    <Directory /api>
        FallbackResource disabled
    </Directory> 
    
    0 讨论(0)
提交回复
热议问题