Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.

Rewrite Rules in AEM Dispatcher are not working for AEM - React SPA

Avatar

Level 3

We are using AEM - React as a SPA.
The Rewrite Rules which are configured for URL shortening were not working with this AEM-React SPA.

We have made some changes in HierarchyPageImpl class so that :path will be specific to the short URL. And the changes are working fine.

But, we have a new use case where we a URL for which there will be different values for a query param. And based on that I need to load a specific page.
Ex:

When users access the below URLs, I need to check the param value and rewrite the rule based on that.

I am able to load those pages but the content for them is still on https://domain/abc page.

https://domain/abc?page=page1

https://domain/abc?page=page2


Below are the Rules I have configured:

RewriteCond %{REQUEST_URI} ^\/abc [NC]
RewriteCond %{QUERY_STRING} ^page=page1 [NC]
RewriteRule ^/(.*)$ /content/page1.html [L,PT]

 

RewriteCond %{REQUEST_URI} ^\/abc [NC]
RewriteCond %{QUERY_STRING} ^page=page2 [NC]
RewriteRule ^/(.*)$ /content/page2.html [L,PT]




I am not able to figure out how this can be handled.

 

Topics

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

17 Replies

Avatar

Community Advisor

Hi @papenaarun 

 

If I understand the requirement, based on query param you want to redirect to some destination?

for both the query param the destination URL is same or different?

 

RewriteCond %{REQUEST_URI} ^/abc
RewriteCond %{QUERY_STRING} ^page=page1$
RewriteRule (.*) /somepage [R=302,L]

RewriteCond %{REQUEST_URI} ^/abc
RewriteCond %{QUERY_STRING} ^page=page2$
RewriteRule (.*) /someotherpageurl [R=302,L]

302 can be updated with 301 once the development is completed.

 

Thanks!

Avatar

Level 3
Not redirect. It should be Rewrite. User end URL shouldn't change. I have already configured and internally page is getting loaded for that. But, the content is not changing as it depends on model.json

Avatar

Community Advisor

Got it. So in both the cases you want to keep the user on the same page but want to show different content based on the query params that are coming as part of the URL?

If that;s the case then you must be using the [PT, L] in the Rewrite rule.

Avatar

Level 3
Yes, I have placed that. You can check in the post above I have updated it. But the issue is with the content of the page.

Avatar

Community Advisor

Hi @papenaarun 

Rewrite Rule should be PT,L and L should be always at the end as from there it will stop processing. I see you are using it as [L, PT]

 

RewriteCond %{REQUEST_URI} ^/abc [NC]
RewriteCond %{QUERY_STRING} ^page=page1 [NC]
RewriteRule ^/(.*)$ /content/page1.html [PT,L]

RewriteCond %{REQUEST_URI} ^/abc [NC]
RewriteCond %{QUERY_STRING} ^page=page2 [NC]
RewriteRule ^/(.*)$ /content/page2.html [PT,L]

 

If the page 1 and page 2 has the content then it will work as there is no other logic that is driven by dispatcher. Please make the above change and try it out.

 

Thanks! 

Avatar

Level 3
This is what happening. While accessing /abc abc.model.json is getting called and the content coming under this is getting loaded in page. After applying rules, still abc.model.json is getting loaded for both the /page1 and /page2

Avatar

Level 3
ideally it has to be page1.model.json for /page1 and page2.model.json for /page2 which we are making from dispatcher

Avatar

Community Advisor

Hi @papenaarun 

 

When you access /content/page1 does it call page1.model.json? If it's calling it then it should call here as well.

 

Can you please try with the below rules once? Made a minor change. Please make the exact rule change as I have mentioned here for each n every line.

 

RewriteCond %{REQUEST_URI} ^/abc [NC]
RewriteCond %{QUERY_STRING} ^page=page1$ [NC]
RewriteRule (.*) /page1 [PT,L]

RewriteCond %{REQUEST_URI} ^/abc [NC]
RewriteCond %{QUERY_STRING} ^page=page2$ [NC]
RewriteRule (.*) /page2 [PT,L]

 

Avatar

Level 3

Yes, I have made the change as you suggested.
Also, when I access /page1 then page1.model.json is getting called, and the same for page2.


Avatar

Community Advisor

Hi,

I think your rules look fine, it just SPA app is not able to find out the pass-through link instead using the original requested link. TEST RESULT



Arun Patidar

Avatar

Level 3
Yes. For SPA entire page content is based on <pagename>model.json. And this will called internally from clientlib-react. While rewriting the URL in dispatcher page will be loaded but the model.json will be of the page which we are accessing from the browser.

Avatar

Community Advisor

@papenaarun - As per my experience with AEM SPA, you need to have one more rewrite rule for model.json as well.

Thanks,
Nikhil Kumar

Avatar

Level 3

Yes, but how do we know which page model.json has to load?

Because I will be receiving the request as abc.model.json

Avatar

Level 1

Hi ,

 

Did you get this resolved? What was the final rule that you added to resolve? Can you also share the full list of dispatcher rules you setup to resolve?