DEV Community

Anh Trần Tuấn
Anh Trần Tuấn

Posted on • Originally published at tuanh.net on

Writing Effective Rewrites in Nginx and Tomcat

1. Rewrites in Nginx

Nginx, known for its high performance and flexibility, uses rewrite rules to modify requests and responses based on specified conditions. Here’s how to implement rewrites in Nginx:

1.1 Basic Rewrite Rule

The simplest form of rewrite rule in Nginx is used to change the URL structure without redirecting the user.

server {
    listen 80;
    server_name example.com;

    location /oldpath {
        rewrite ^/oldpath/(.*)$ /newpath/$1 permanent;
    }

    location /newpath {
        # Configuration for the new path
    }
}
Enter fullscreen mode Exit fullscreen mode

rewrite ^/oldpath/(.*)$ /newpath/$1 permanent;: This line tells Nginx to redirect requests from /oldpath to /newpath while preserving the rest of the URL path. The permanent keyword specifies a 301 redirect.

1.2 Conditional Rewrites

You might want to apply rewrites based on specific conditions, such as the user's location or request headers.

server {
    listen 80;
    server_name example.com;

    location / {
        if ($http_user_agent ~* "Mobile") {
            rewrite ^/desktop/(.*)$ /mobile/$1 break;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

if ($http_user_agent ~* "Mobile"): This condition checks if the user agent contains "Mobile."

rewrite ^/desktop/(.*)$ /mobile/$1 break;: Rewrites requests from /desktop to /mobile for mobile users.

1.3 Testing Rewrites

To test your Nginx rewrite rules, use the following command to check for syntax errors:

nginx -t
Enter fullscreen mode Exit fullscreen mode

Reload Nginx to apply the changes:

systemctl reload nginx
Enter fullscreen mode Exit fullscreen mode

2. Rewrites in Tomcat

Tomcat, a widely used Java servlet container, also supports URL rewriting, though it requires different configurations compared to Nginx. The main approach is through the web.xml file or using the Tomcat rewrite valve.

Image

2.1 Configuring URL Rewrites with web.xml

You can use Tomcat's web.xml to define URL patterns and map them to servlets.

<servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>com.example.MyServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/newpath/*</url-pattern>
</servlet-mapping>

<filter>
    <filter-name>RewriteFilter</filter-name>
    <filter-class>org.apache.catalina.filters.RewriteFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>RewriteFilter</filter-name>
    <url-pattern>/oldpath/*</url-pattern>
</filter-mapping>
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • Define a servlet and map it to a URL pattern.
  • Use a rewrite filter to apply the rules to incoming requests.

2.2 Using Tomcat's Rewrite Valve

Tomcat’s rewrite valve provides more advanced rewrite capabilities.

Example : Add the following configuration in server.xml:

<Valve className="org.apache.catalina.valves.RewriteValve" />
Enter fullscreen mode Exit fullscreen mode

Rewrite rules can then be defined in a separate rewrite.config file:

RewriteRule ^/oldpath/(.*)$ /newpath/$1 [R=301,L]
Enter fullscreen mode Exit fullscreen mode

RewriteRule ^/oldpath/(.*)$ /newpath/$1 [R=301,L]: This rule performs a 301 redirect from /oldpath to /newpath.

2.3 Testing Rewrites

Restart Tomcat to apply the new configuration:

systemctl restart tomcat
Enter fullscreen mode Exit fullscreen mode

3. Conclusion

Understanding and implementing rewrites in both Nginx and Tomcat can significantly enhance your web server's functionality and user experience. By following the techniques outlined in this guide, you can effectively manage URL redirections and ensure that your web server handles requests as intended.

If you have any questions or need further clarification, feel free to leave a comment below!

Read posts more at : Writing Effective Rewrites in Nginx and Tomcat

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

While many AI coding tools operate as simple command-response systems, Qodo Gen 1.0 represents the next generation: autonomous, multi-step problem-solving agents that work alongside you.

Read full post

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay