Home >System Tutorial >LINUX >Teach you Tomcat URL rewriting

Teach you Tomcat URL rewriting

PHPz
PHPzOriginal
2024-06-13 19:29:03717browse

教你Tomcat URL重写

前期准备
安装JDK
yum install java-1.8.0-openjdk-devel

vim /etc/profile.d/jdk.sh
export JAVA_HOME=/usr/java/latest                   # 首先定义JAVA_HOME的环境变量
export PATH=$JAVA_HOME/bin:$PATH                    # 然后追加

.  /etc/profile.d/jdk.sh
安装tomcat
yum install tomcat tomcat-admin-webapps.noarch \
tomcat-webapps.noarch tomcat-docs-webapp.noarch
urlRewriteFilter实现重定向

urlRewriteFilter是一个用于改写URL的Web过滤器,类似于Apache的mod_rewrite。适用于任何Web应用服务器(如Tomcat,jboss,jetty,Resin,Orion等)。其典型应用就把动态URL静态化,便于搜索引擎爬虫抓取你的动态网页。

下载UrlRewriteFilter

UrlRewriteFilter的官方网站http://tuckey.org/urlrewrite/

jar包要放在应用的WEB-INF/lib目录中

yum install wget -y
cd /usr/share/tomcat/webapps/ROOT/WEB-INF/lib/
wget http://central.maven.org/maven2/org/tuckey/urlrewritefilter/4.0.3/urlrewritefilter-4.0.3.jar
配置过滤规则

在应用的./WEB-INF/目录下创建一个名为urlrewrite.xml规则文件,名称千万不能出错!!

vim ./WEB-INF/urlrewrite.xml
      <rule>
          <name>seo redirect</name>
          <condition name="host" operator="notequal">^www.example.com</condition>
          <condition name="host" operator="notequal">^localhost</condition>
          <from>^/.*</from>
          <to type="redirect" last="true">http://www.example.com/$1</to>
      </rule>
配置tomcat

在应用的`./WEB-INF/中的web.xml中添加

vim ./WEB-INF/web.xml
      <filter>
      </filter><filter>UrlRewriteFilter</filter>
      <filter>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter>
    
    <filter>
      </filter><filter>UrlRewriteFilter</filter>
      <url>/*</url>
      <dispatcher>REQUEST</dispatcher>
      <dispatcher>FORWARD</dispatcher>
    

The above is the detailed content of Teach you Tomcat URL rewriting. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn