15

Question: how to configure .htaccess in spring boot?

angularjs provides html5Mode, which makes your app use pushstate-based URL instead of hashtags. However this requires server side support, since the generated urls need to be rendered properly as well.

If you're running your angular app on an apache server you can easily add this rule in an .htaccess file.

# Apache .htaccess

# angularjs pushstate (history) support:
# See http://www.josscrowcroft.com/2012/code/htaccess-for-html5-history-pushstate-url-routing/
<ifModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !index
    RewriteCond %{REQUEST_URI} !.*\.(css¦js|html|png) #Add extra extensions needed.
    RewriteRule (.*) index.html [L]
</ifModule>
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
Igor Roman
  • 221
  • 3
  • 7
  • 3
    See my answer here: http://stackoverflow.com/questions/30859231/angular-html5-urls-server-configuration/37878557#37878557 – Christian Laakmann Jun 17 '16 at 10:00
  • 1
    Does this answer your question? [Angular HTML5 URLs - Server configuration](https://stackoverflow.com/questions/30859231/angular-html5-urls-server-configuration) – Scott Frederick Oct 28 '20 at 14:31

1 Answers1

3

Spring Boot is typically used to create RESTful web services or web applications using Java. Unlike traditional web development using technologies like PHP, Apache and .htaccess files are not commonly used in Spring Boot.

However, if you still want to use .htaccess with Spring Boot, you can configure it by doing the following steps:

Create a .htaccess file in the src/main/resources folder of your Spring Boot application.

Add the configuration rules to the .htaccess file that you want to apply to your application. For example, if you want to redirect all HTTP requests to HTTPS, you can use the following rule:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Add the following dependency to your pom.xml file:

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-core</artifactId>
    <version>9.0.43</version>
</dependency>

Add the following configuration to your application.properties file:

server.tomcat.internal-proxies=.*

This configuration tells Tomcat to treat requests from localhost as internal requests, which allows the .htaccess file to be processed.

Note that using .htaccess with Spring Boot is not recommended, as it can impact performance and is not a standard practice in Spring Boot development. It is better to use Spring Boot's built-in security features and configuration options for handling web requests.