tomcat重写,配置读取但不工作

标签 tomcat url-rewriting

我正在尝试获取 URL localhost/article/article-title 来“执行”URL localhost/getArticle?文章标题。

我认为我已经正确配置了阀门,因为我在日志中看到了这一点:

03-Apr-2016 01:11:38.206 FINE [Catalina-startStop-1] org.apache.catalina.valves.rewrite.RewriteValve.startInternal Read configuration from /usr/local/apache-tomcat-8.0.33/conf/Catalina/localhost/rewrite.config
03-Apr-2016 01:11:38.208 FINE [Catalina-startStop-1] org.apache.catalina.valves.rewrite.RewriteValve.parse Add rule with pattern ^article/(.*) and substitution getArticle?$1
03-Apr-2016 01:11:38.775 FINE [localhost-startStop-1] org.apache.catalina.valves.rewrite.RewriteValve.startInternal Read configuration from: /WEB-INF/rewrite.config
03-Apr-2016 01:11:38.775 FINE [localhost-startStop-1] org.apache.catalina.valves.rewrite.RewriteValve.parse Add rule with pattern ^article/(.*) and substitution getArticle?$1

我已经在 server.xml 和 context.xml 中激活了它。读取文件 rewrite.config 是因为在日志中,您可以看到:

Add rule with pattern ^article/(.*) and substitution getArticle?$1

这是我的 rewrite.config 的内容:

#To make it work, active the Rewrite Valve is ../META-INF/context.xml 
RewriteRule ^article/(.*) getArticle?$1 [L]

但是当我去 http://localhost/article/article-title ,我在日志中得到了这个:

27.0.0.1 - - [03/Apr/2016:01:12:01 +0200] "GET /article/article-title HTTP/1.1" 404 1036

如何确定重写已激活?

感谢您的宝贵时间。

最佳答案

RewriteRule中要匹配的路径是相对于当前容器的。所以 RewriteRule 应该是:

RewriteRule ^/article-(.*) getArticle?$1 [L]

关于tomcat重写,配置读取但不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36379898/

相关文章:

apache - 从 example.com 重定向到 www.example.com

apache - 复杂的 .htaccess 重写

api - REST - 支持多种可能的标识符

java - *.war 文件无法在 Tomcat 上创建任何内容的主要原因是什么?

spring - 在 Hybris 控制台中处理警告

java - 取消部署我的应用程序时,Tomcat 抛出 java.lang.UnsupportedOperationException

iis - 如何移动页面并重命名它们,同时不破坏来自仍然使用格式不正确的 URL 的外部站点的传入链接

mysql - 没有找到适合 jdbc mysql 的驱动程序?

tomcat - maven tomcat 部署插件,这样就不会完成重建

.htaccess - 如何使用 .htaccess 从 URL 中删除 .cgi?