Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Apache Rewrite rules for hiding servlet paths

Avatar

Level 4

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!

 

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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 

imran__khan_1-1708612948134.png

 

<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>

 

View solution in original post

4 Replies

Avatar

Community Advisor

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]

Avatar

Level 4

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

Avatar

Correct answer by
Community Advisor

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 

imran__khan_1-1708612948134.png

 

<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>

 

Avatar

Level 4

Hi @Imran__Khan ,

 

It's my bad , those rules didn't saved in my rules file hence rewrites were not working. Now I have saved them properly and restarted httpd then it worked. below is the updated one.

 

 

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

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

 

 

I have removed QSA flag as it's not required because query params are automatically transferring to actual API. Thanks @Imran__Khan