title: Rewrite Plugin keywords: [“rewrite”] description: rewrite plugin

1. Overview

1.1 Plugin Name

  • Rewrite Plugin

1.2 Appropriate Scenario

  • The request uri can be different from the target service by rewriting the path.

1.3 Plugin functionality

  • This plugin is used to rewrite the request uri.

1.4 Plugin code

  • Core Module shenyu-plugin-rewrite

  • Core Class org.apache.shenyu.plugin.rewrite.RewritePlugin

1.5 Added Since Which shenyu version

  • Since ShenYu 2.4.0

2. How to use plugin

2.1 Plugin-use procedure chart

2.2 Import pom

  • Import maven config in shenyu-bootstrap project's pom.xml file..
  <dependency>
      <groupId>org.apache.shenyu</groupId>
      <artifactId>shenyu-spring-boot-starter-plugin-rewrite</artifactId>
      <version>${project.version}</version>
  </dependency>

2.3 Enable plugin

  • In shenyu-admin --> BasicConfig --> Plugin --> rewrite set Status enabled.

2.4 Config plugin

2.4.1 Plugin Config

  • Enable the plugin before using.
  • Disable the plugin if don't use.

2.4.2 Selector Config

2.4.3 Rule Config

  • Param details:
    • regex: The regular expression that matches the part of uri to be rewrited.
    • replace: The content of replacement.
    • percentage : The percentage of rewriting, 100 represents 100%.
    • rewriteMetaData: Whether to rewrite metadata, true indicates that it is enabled, and once enabled, the uri can be rewritten across plugins.

2.5 Examples

2.5.1 Example for rewriting uri

2.5.1.1 Run the shenyu-examples-http project

2.5.1.1 Plugin Config

  • Refer to 2.4.1 to configure plugin.

2.5.1.2 Selector Config

  • Refer to 2.4.2 to configure selector

2.5.1.3 Rule Config

The request /http/hello would be rewritten to /hi

2.5.1.4 Check Result

Use some tool (such as Postman) to make a request:

3. How to disable plugin

  • In shenyu-admin --> BasicConfig --> Plugin --> rewrite set Status disable.