Panini Luncher
Panini Luncher

Reputation: 649

Tomcat Rewrite Valve - How to Keep Pretty URL on Client Browser

I'm using Tomcat Rewrite Valve API so that I could publish my webapp URLs as 'pretty URL'. My issue is that after successful URL rewriting, the browser url address did not keep the pretty URL, but display to the final ugly URL that I tried to hide in the first place, therefore defeating the purpose of using this nice API. I understand with Apache mod_rewrite module, the browser would keep the pretty URL that user enters after the successful rewrite; hence hiding nitty-gritty URI and params from naked eyes. Is it possible to achieve this with Rewrite Valve API? How?

Configs

  1. Server = Tomcat Apache 9.0.30

  2. [webapp]/META-INF/context.xml

<Context override="true" > <Valve className="org.apache.catalina.valves.rewrite.RewriteValve" /> </Context>

  1. [webapp]/WEB-INF/rewrite.config

    RewriteRule ^/support/contactUs/?$ ?page=page_supportID&fragment=fragment_contactUsID [NC,L]

Use-Case: User enters on their browser pretty URL: http://example.com/support/contactUs

Expected: The rewrite rule applies and opens the actual (ugly) URL = http://example.com?page=page_supportID&fragment=fragment_contactUsID, and user browser still displays pretty URL = http://example.com/support/contactUs.

Actual: The rewrite rule applied and opened the actual (ugly) URL = http://example.com?page=page_supportID&fragment=fragment_contactUsID. HOWEVER the browser url address also displayed that ugly url to user, instead of keeping the pretty one.

Upvotes: 2

Views: 2752

Answers (1)

Panini Luncher
Panini Luncher

Reputation: 649

NOT possible!

I wish I was wrong, however! After studying more of this behavior, for the URL Rewrite scheme-of-thing, we may not be able to control how client browser would handle and display after-redirecting URL on its url bar using this API.

Tomcat RewriteValve API has done what it is supposed to do = By DEFAULT returning proper HTTP status code 302 back to the client while redirecting the URL. Per Tomcat doc:

If no code is given, an HTTP response of 302 (FOUND, previously MOVED TEMPORARILY) will be returned.

We can confrm by reading network HTTP request/response stacktrace. For the purpose of this question, HTTP response 302 is what we expected to see. Per MDN doc:

HTTP 302 Found ... A browser redirects to this page but search engines don't update their links to the resource ... Even if the spec requires the method (and the body) not to be altered when the redirection is performed, not all user-agents conform here - you can still find this type of bugged software out there.

O welp!

Upvotes: 0

Related Questions