AngularJS인 경우 설정 (** html에 <base href="/"> 추가)
1 2 3 | angular.config(['$locationProvider'], function ($locationProvider) { $locationProvider.html5Mode(true); }); |
Angular인 경우 설정 (** html에 <base href="/"> 추가)
1 2 3 4 5 6 | @NgModule({ imports: [ RouterModule.forRoot(LAYOUT_ROUTES, { useHash: false }) ], ... }) |
UrlRewriteFilter 설정 (Spring Framework, Gradle)
- 의존성 설정
compile("org.tuckey:urlrewritefilter:${urlrewritefilter_version}")
- Custom UrlRewriteFilter 생성 (url-rewrite.xml 위치를 변경을 위해)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.tuckey.web.filters.urlrewrite.Conf; import org.tuckey.web.filters.urlrewrite.utils.StringUtils; import javax.servlet.FilterConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; import java.io.InputStream; import java.net.URL; /** * Subclass of {@link org.tuckey.web.filters.urlrewrite.UrlRewriteFilter} * that overrides the configuration file loading mechanism. */ public class UrlRewriteFilter extends org.tuckey.web.filters.urlrewrite.UrlRewriteFilter { private final Logger log = LoggerFactory.getLogger(UrlRewriteFilter.class); @Override protected void loadUrlRewriter(FilterConfig filterConfig) throws ServletException { ServletContext context = filterConfig.getServletContext(); String confPath = filterConfig.getInitParameter("confPath"); boolean modRewriteStyleConf = getModRewriteStyleConf(filterConfig.getInitParameter("modRewriteConf")); try { final InputStream inputStream = getClass().getClassLoader().getResourceAsStream(confPath); final URL confUrl = getClass().getClassLoader().getResource(confPath); if (inputStream == null) { log.error("unable to find urlrewrite conf file at " + confPath); // set the writer back to null if (isLoaded()) { log.error("unloading existing conf"); destroyUrlRewriter(); } } else { Conf conf = new Conf(context, inputStream, confPath, confUrl.toString(), modRewriteStyleConf); checkConf(conf); } } catch (Throwable e) { log.error("unloading urlrewrite conf file at " + confPath, e); throw new ServletException(e); } } private boolean getModRewriteStyleConf(String modRewriteConf) { boolean modRewriteStyleConf = false; if (!StringUtils.isBlank(modRewriteConf)) { modRewriteStyleConf = "true".equals(StringUtils.trim(modRewriteConf).toLowerCase()); } return modRewriteStyleConf; } } |
- Servlet Filter에 넣기
1 2 3 4 5 6 7 8 9 10 11 | private void initUrlRewriteFilter(ServletContext servletContext, EnumSet<DispatcherType> disps) { FilterRegistration.Dynamic urlRewriteFilter = servletContext.addFilter("urlRewriteFilter", new UrlRewriteFilter()); urlRewriteFilter.setInitParameter("confPath", "url-rewrite.xml"); urlRewriteFilter.setInitParameter("confReloadLastCheck", "-1"); urlRewriteFilter.setInitParameter("logLevel", "WARM"); urlRewriteFilter.addMappingForUrlPatterns(disps, true, "/*"); urlRewriteFilter.setAsyncSupported(true); } |
- url-rewrite.xml 작성 (위치: src/main/resources)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN" "https://www.tuckey.org/res/dtds/urlrewrite4.0.dtd"> <urlrewrite > <!-- default --> <rule match-type="regex"> <condition type="request-uri" operator="notequal"> ^.*\.(html?|htc|js|css|xml|map|json|ico|bmp|gif|jpe?g|png|svg|eot|woff|woff2|ttf|pdf|swf|txt)$ </condition> <from>^/(.*)$</from> <to last="true">/index.html</to> </rule> </urlrewrite> |
참고
- https://github.com/spring-io/sagan/search?utf8=%E2%9C%93&q=urlrewrite.xml&type=