Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn more

View all

Sign in to view all badges

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

papenaarun
Level 2
Level 2

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.

 

AEM 6.5 React JS Rewrite Rules SPA
14 Replies
asutosh_jena
Community Advisor
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!

papenaarun
Level 2
Level 2
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
asutosh_jena
Community Advisor
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.

papenaarun
Level 2
Level 2
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.
asutosh_jena
Community Advisor
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! 

papenaarun
Level 2
Level 2
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
papenaarun
Level 2
Level 2
ideally it has to be page1.model.json for /page1 and page2.model.json for /page2 which we are making from dispatcher
asutosh_jena
Community Advisor
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]

 

papenaarun
Level 2
Level 2

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.


Arun_Patidar
Community Advisor
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

papenaarun
Level 2
Level 2
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.
Nikhil-Kumar
Community Advisor
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

papenaarun
Level 2
Level 2

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

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