【问题标题】:Enable mod_rewrite in Windows for .htaccess to work在 Windows 中启用 mod_rewrite 以使 .htaccess 工作
【发布时间】:2018-11-14 12:35:09
【问题描述】:

我想在 Apache Tomcat 服务器上运行我的 Angular 6 应用程序。但是每次我在深层链接 URL 中重新加载时,它都会显示状态 404 page not found。 因此,为此,我将 .htaccess 文件包含在我的 index.html 所在的根文件夹中。 但是,它仍然不起作用,因为可能没有启用 mod_rewrite。

为了启用 mod_rewrite,我正在搜索 httpd.conf 文件,我必须在其中进行更改。但是在我的 'C:\Program Files\Apache Software Foundation\Tomcat 9.0' 目录中没有这样的文件。

那么我如何启用 mod_rewrite 以使 .htaccess 正常工作并重新加载我的 Angular 应用程序而不会出错?

【问题讨论】:

    标签: apache .htaccess tomcat


    【解决方案1】:

    Tomcat 不使用 httpd.conf,它是一个 apache 文件。各个 webapp 的位置保存在它们各自的 web.xml 文件中,但所有配置的位置都在 ../tomcat8/conf/server.xml 和 web.xml 中

    尝试 WAMP 或 Apache2 而不是 Tomcat

    您需要指示 .htaccess 文件在每次点击时加载 index.html。

    喜欢

    DirectoryIndex index.html
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-s
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !\.(jpg|jpeg|png|gif|flv|mpeg|mpg|mp4)$
    RewriteRule ^.*$ bootstrap.html [NC,L]
    

    您将在httpd.conf 文件中的LoadModule rewrite_module modules/mod_rewrite.so 中找到此行,该文件将位于conf 文件夹下。您需要从中删除哈希(#),然后重新启动 apache 服务。

    如果你想在 Tomcat 中进行设置,那么我在示例项目中所做的就是在 webapps\ROOT\WEB-INF\web.xml 文件中添加以下代码

    <?xml version="1.0" encoding="UTF-8"?>
    <!--
     Licensed to the Apache Software Foundation (ASF) under one or more
      contributor license agreements.  See the NOTICE file distributed with
      this work for additional information regarding copyright ownership.
      The ASF licenses this file to You under the Apache License, Version 2.0
      (the "License"); you may not use this file except in compliance with
      the License.  You may obtain a copy of the License at
    
          http://www.apache.org/licenses/LICENSE-2.0
    
      Unless required by applicable law or agreed to in writing, software
      distributed under the License is distributed on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      See the License for the specific language governing permissions and
      limitations under the License.
    -->
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                          http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
      version="4.0"
      metadata-complete="true">
    
      <display-name>Welcome to Tomcat</display-name>
      <description>
         Welcome to Tomcat
      </description>
        <welcome-file-list>
            <welcome-file>index.html</welcome-file>
        </welcome-file-list>
    
       <error-page>
        <error-code>404</error-code>
        <location>/test/index.html</location>
      </error-page>
    </web-app>
    

    这里/test/index.htmltest是我的项目名称,你可以改成你的,但尽量使用项目web.xml来避免其他项目重定向。在这种情况下,您可以直接在路径中添加/index.html

    【讨论】:

    • conf文件夹下没有这样的文件。我检查了很多次。事实上,我提到的目录中没有像 httpd.conf 这样的文件。那么如何启用 mod_rewrite 呢?
    • 你有这个结构吗? conf/virtual-hosts/default.conf ?
    • 或者在这里找到类似conf/Catalina/my.domain.com/rewrite.config my.domain.com 的例子
    • 或者你可以使用 WAMP 或 Apache2 代替 Tomcat
    • 我已经更新了答案,它测试了它。让我知道这是否有效
    猜你喜欢
    • 2017-07-14
    • 2017-06-12
    • 2010-10-31
    • 2011-07-31
    • 2015-12-02
    • 2015-03-28
    • 1970-01-01
    • 2012-08-05
    • 1970-01-01
    相关资源
    最近更新 更多