Rewrite rules causing model.json to be requested twice | Community
Skip to main content
September 10, 2024

Rewrite rules causing model.json to be requested twice

  • September 10, 2024
  • 2 replies
  • 1649 views

Hi,

 

We are setting up a new project, where the developers are making use of the AEM JSON exporter to create a file with the name "pagename.model.json" for an incoming request, which is then used by the Angular code on the browser to render the webpage. 

 

I have added a rewrite rule to mask the path "/content/<project-name>" in the URL with the following rule.

 

RewriteRule ^/(.*) /content/<project-name>/$1 [PT]

 

The rewrite is working as expected for the HTML pages, however, the model.json files are being called twice once without the "/content/<project-name>" and once with it, which is causing a couple of issues in the page rendering.

 

Is there any way to eliminate the call with the content path?

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

2 replies

konstantyn_diachenko
Community Advisor
Community Advisor
September 10, 2024

Hi @ashish_kamalaksha,

 

Unfortunately, we need more details about all other rewrite rules. I would suggest to investigate rewrite rules locally by increasing rewrite log level in the apache configuration.

If you are running on AEMaaCS, you can define REWRITE_LOG_LEVEL variable:

REWRITE_LOG_LEVEL=trace6

If you are running on custom Apache server add this line to the vhost configuration (https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html 😞

LogLevel alert rewrite:trace6
Kostiantyn Diachenko, Community Advisor, Certified Senior AEM Developer, creator of free AEM VLT Tool, maintainer of AEM Tools plugin.
September 11, 2024

HI @konstantyn_diachenko 

 

We currently only have 2 redirection rules in place. 

1. RewriteRule ^/$ /content/<project-name>/home.html [PT,L] ( To redirect / to the home page internally )
2. 

RewriteCond %{REQUEST_URI} !^/apps/(.*) [NC]
RewriteCond %{REQUEST_URI} !^/bin/(.*) [NC]
RewriteCond %{REQUEST_URI} !^/etc(.*) [NC]
RewriteCond %{REQUEST_URI} !^/libs(.*) [NC]

RewriteCond %{REQUEST_URI} !^/content(.*) [NC]
RewriteCond %{REQUEST_URI} !^/system(.*) [NC]
RewriteCond %{REQUEST_URI} !^/dam(.*) [NC]
RewriteCond %{REQUEST_URI} !^/server-status [NC]
RewriteCond %{REQUEST_URI} !^/dispatcher/invalidate.cache [NC]
RewriteCond %{REQUEST_URI} !^/$

RewriteRule ^/(.*) /content/<project-name>/home/$1 [PT,L] (To mask /content/<project-name>/ in all requests that are under this path)

 

I have enabled the higher log level for rewrite, and also checked access logs. It seems the first request is for model.json with the content path and then for the one without. 

 

For example for the path "/", it is successfully redirecting internally to the expected homepage. Then a request for /content/<project-name>/home.html is received followed shortly by a request for /.model.json.

konstantyn_diachenko
Community Advisor
Community Advisor
September 11, 2024

Try to add explicit following rules between 1st and 2nd rule for testing:

RewriteRule ^(.*).model.json /content/<project-name>/home$1.model.json [PT,L] RewriteRule ^/.model.json /content/<project-name>/home.model.json [PT,L]

 

I see that you 2nd rule starts with /.

In this case case the path /child-page.html will be rewritten to go further to AEM with /content/<project-name>/home/child-page.html. A /child-page.model.json ->  /content/<project-name>/home/child-page.model.html.

But if you open home page (just /), your code will send /.model.json request and it will be rewritten to /content/<project-name>/home/.model.html that is wrong. Try to test dispatcher with 2 explicit rules above.

 

Hope it helps.

 

 

Kostiantyn Diachenko, Community Advisor, Certified Senior AEM Developer, creator of free AEM VLT Tool, maintainer of AEM Tools plugin.
Shashi_Mulugu
Community Advisor
Community Advisor
September 11, 2024

@ashish_kamalaksha as said by @konstantyn_diachenko we might need more details..

 

But few things to look for

More than one rewrite rule matching , for this use L apache flag which marks the rule as last rewrite and doesn't execute subsequent rules

 

Check if your publish farms are multiple and all are executed.

September 11, 2024

Hi @shashi_mulugu 

 

Currently, we only have a dev setup ( 1 each of author, publisher and dispatcher ) and only 2 redirect rules at the moment. 

 

1. RewriteRule ^/$ /content/<project-name>/home.html [PT,L] ( To redirect / to the home page internally )
2. 

RewriteCond %{REQUEST_URI} !^/apps/(.*) [NC]
RewriteCond %{REQUEST_URI} !^/bin/(.*) [NC]
RewriteCond %{REQUEST_URI} !^/etc(.*) [NC]
RewriteCond %{REQUEST_URI} !^/libs(.*) [NC]

RewriteCond %{REQUEST_URI} !^/content(.*) [NC]
RewriteCond %{REQUEST_URI} !^/system(.*) [NC]
RewriteCond %{REQUEST_URI} !^/dam(.*) [NC]
RewriteCond %{REQUEST_URI} !^/server-status [NC]
RewriteCond %{REQUEST_URI} !^/dispatcher/invalidate.cache [NC]
RewriteCond %{REQUEST_URI} !^/$

RewriteRule ^/(.*) /content/<project-name>/home/$1 [PT,L] (To mask /content/<project-name>/ in all requests that are under this path)

 

I have enabled the higher log level for rewrite, and also checked access logs. It seems the first request is for model.json with the content path and then for the one without. 

 

For example for the path "/", it is successfully redirecting internally to the expected homepage. Then a request for /content/<project-name>/home.html is received followed shortly by a request for /.model.json.

Shashi_Mulugu
Community Advisor
Community Advisor
September 11, 2024

Got it, please not condition for model.json in your regular rules and create a new rule only for model.json