Apache Rewrite rules for hiding servlet paths | Community
Skip to main content
Uppari_Ramesh
February 22, 2024
Solved

Apache Rewrite rules for hiding servlet paths

  • February 22, 2024
  • 1 reply
  • 1020 views

Hi Team, @estebanbustamante @raja_reddy ,

 

I want to hide my servlet paths so that I need to write rewrites at dispatcher.

 

Currently client is calling the api in below format 

 

Path Based: http://localhost:4502/bin/test/v1/content?type=page

Resource basedhttp://localhost:4502/content/test/servlets/fruits.banana.json?type=58&color=yellow

 

here fruits is a servlet resource node and sling:resourceType is fruits/test/servlets.

I have registered servlet resource type as fruits/test/servlets and with selectors banana and extension as json. Like this I have many servlets with same resource type and multiple selectors.

 

I want to mask actual servlet URLs so that,

 

now the client will call API in below format.

Path Basedhttp://localhost:4502/api/v1/content?type=page  

The above URL should internally call original servlet i.e. http://localhost:4502/bin/test/v1/content?type=page

 

Resource based: http://localhost:4502/mask.banana.json?type=58&color=yellow

The above URL should internally call original servlet i.e. http://localhost:4502/content/test/servlets/fruits.banana.json?type=58&color=yellow

 

I am writing rewrites in mod_rewrite module as below. 

 

 

Path based: RewriteRule ^/api/v1/(.*)$ /bin/test/v1/$1?type=page [QSA,PT,L]

Resource based: RewriteRule ^/mask.(.*)$ http://localhost:4502/content/test/servlets/fruits.$1?type=58&color=yellow [QSA,PT,L]

 

Using QSA flag to pass the query params from proxy servlet to actual.

 

The above rules not giving expected result. Please suggest how can I achieve this

 

Thanks!

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Imran__Khan

Please hit and see if /content/test/servlets/fruits.banana.json working for you.
Also, check path should be allowed and json as an extension is also must be allowed in your dispatcher.

Above rule is working in my local 

 

<VirtualHost *:80> <Directory "C:/location"> <IfModule disp_apache2.c> ModMimeUsePathInfo On # enable dispatcher for ALL request. if this is too restrictive, # move it to another location SetHandler dispatcher-handler </IfModule> Options FollowSymLinks Includes AllowOverride None AddOutputFilter INCLUDES .html </Directory> ProxyRequests off ProxyPreserveHost On LimitRequestFieldSize 32768 AllowEncodedSlashes NoDecode RewriteEngine On RewriteRule ^/mask\.(.*)$ /content/test/servlets/fruits.$1 [QSA,PT,L] </VirtualHost>

 

1 reply

Imran__Khan
Community Advisor
Community Advisor
February 22, 2024

To achieve the desired masking of servlet URLs using Apache's mod_rewrite module, you can modify your rewrite rules as follows:

# Path based masking
RewriteRule ^/api/v1/(.*)$ /bin/test/v1/$1?type=page [QSA,PT,L]

# Resource based masking
RewriteRule ^/mask\.(.*)$ /content/test/servlets/fruits.$1 [QSA,PT,L]

Uppari_Ramesh
February 22, 2024

Hi @imran__khan ,

 

I tried the rules but I am getting below error

 

Resource at '/mask.banana.json' not found: No resource found

Cannot serve request to /mask.banana.json in /apps/sling/servlet/errorhandler/404.jsp

Imran__Khan
Community Advisor
Imran__KhanCommunity AdvisorAccepted solution
Community Advisor
February 22, 2024

Please hit and see if /content/test/servlets/fruits.banana.json working for you.
Also, check path should be allowed and json as an extension is also must be allowed in your dispatcher.

Above rule is working in my local 

 

<VirtualHost *:80> <Directory "C:/location"> <IfModule disp_apache2.c> ModMimeUsePathInfo On # enable dispatcher for ALL request. if this is too restrictive, # move it to another location SetHandler dispatcher-handler </IfModule> Options FollowSymLinks Includes AllowOverride None AddOutputFilter INCLUDES .html </Directory> ProxyRequests off ProxyPreserveHost On LimitRequestFieldSize 32768 AllowEncodedSlashes NoDecode RewriteEngine On RewriteRule ^/mask\.(.*)$ /content/test/servlets/fruits.$1 [QSA,PT,L] </VirtualHost>