【问题标题】:angular html5mode in tomcat apache 8.5.4 gives 404 on page refreshtomcat apache 8.5.4中的角度html5mode在页面刷新时给出404
【发布时间】:2017-01-20 23:07:31
【问题描述】:

我正在使用 gulp-war 创建 war 文件并将其部署到我系统上的 tomcat apache server 8.5.4 中

它首先在 localhost:8080/projectName/ 上运行

但每当我刷新页面时,它都会出现 404 错误。如何解决这个问题?

如果我从配置文件中删除 $locationProvider.html5Mode(true); 并创建 war 文件而不是它工作正常

一切正常,但 URl 附带 /#/

我尝试添加.htaccess 文件但没有区别

谁有解决办法?

【问题讨论】:

    标签: angularjs tomcat


    【解决方案1】:

    我在this article的帮助下找到了解决方案

    1. 添加到/opt/tomcat/conf/context.xml

       <!-- REWRITE VALVE -->
       <Valve className="org.apache.catalina.valves.rewrite.RewriteValve" />
       <!-- // -->  
      
    2. 在特定项目 WEB-INF 文件夹中添加 rewite.config 文件,其内容如下,例如 /opt/tomcat/webapps/projectName/WEB-INF /rewrite.config

      RewriteCond %{REQUEST_URI} !^.*\.(bmp|css|gif|htc|html?|ico|jpe?g|js|pdf|png|swf|txt|xml|svg|eot|woff|woff2|ttf|map)$
      RewriteRule ^(.*)$ /index.html [L]
      

    希望对某人有所帮助

    【讨论】:

    • 也可以在你的 RewriteCond 中做相反的事情,只接受以没有扩展名或 html 结尾的 url:RewriteCond %{REQUEST_URI} ^[^.]*(html?)?$ 你可以添加其他页面类型,如 ^[^.]*(html?|jsp|aspx)?$ 这样你就不必每次需要添加新文件类型时,请继续编辑 rewrite.conf。
    • 本地部署时,tomcat webapps里面没有项目文件夹。您是否从 IDE 执行了第 2 步?
    【解决方案2】:

    这是全局方法的示例:

    <?xml version='1.0' encoding='utf-8'?>
    <!-- The contents of this file will be loaded for each web application -->
    <Context>
        <!-- REWRITE VALVE -->
        <Valve className="org.apache.catalina.valves.rewrite.RewriteValve" />
        <!-- // -->
    
        <!-- Speed up context loading -->
        <JarScanner scanClassPath="false" />
        <!-- Default set of monitored resources. If one of these changes, the -->
        <!-- web application will be reloaded.                                 -->
        <WatchedResource>WEB-INF/web.xml</WatchedResource>
    
        <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
    </Context>
    

    Just jave 在您的列表中添加了两个最受欢迎的扩展:

    RewriteCond %{REQUEST_URI} !^.*\.(bmp|css|gif|htc|html?|ico|jpe?g|js|pdf|png|swf|txt|xml|svg|eot|woff|woff2|ttf|map|json)$
    RewriteRule ^(.*)$ /index.html [L]
    

    我喜欢反向方法(由 评论),但更喜欢保持简洁和可读性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-21
      • 2013-10-29
      • 2018-12-26
      • 1970-01-01
      • 1970-01-01
      • 2016-05-30
      • 2017-01-01
      • 2015-04-21
      相关资源
      最近更新 更多