Enable mod_rewrite in Windows for .htaccess to work

Viewed 921

I want to run my Angular 6 app over Apache Tomcat server. But every time I reload when in deep link URL then it shows status 404 page not found. So, for that, I included the .htaccess file in the root folder where my index.html is. But still, it doesn't work because maybe the mod_rewrite is not enabled.

To enable mod_rewrite I was searching for httpd.conf file where I have to make the changes. But in my 'C:\Program Files\Apache Software Foundation\Tomcat 9.0' directory there is NO such file.

So how do i enable the mod_rewrite to make .htaccess work and reload my Angular app without an error?

1 Answers

Tomcat doesn't use httpd.conf, that is an apache file. The location of the individual webapps are kept in their individual web.xml files, but the location of all the configs are in ../tomcat8/conf/server.xml and web.xml

Try WAMP or Apache2 instead of Tomcat

You need to direct the .htaccess file to load the index.html on each hit.

like

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]

You will found this line in LoadModule rewrite_module modules/mod_rewrite.so in httpd.conf file, the file will be under the conf folder. you need to remove the hash(#) from it and then restart the apache services.

If you want that setup in Tomcat, then what I did in sample project is added the below code in webapps\ROOT\WEB-INF\web.xml file

<?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>

Here /test/index.html test is my project name, you can change it to yours but try to use the project web.xml to avoid other project redirection. and in that case you can directly add the /index.html in path

Related