How to prevent a webpage from opening directly? | Community
Skip to main content
Level 2
August 2, 2022
Solved

How to prevent a webpage from opening directly?

  • August 2, 2022
  • 1 reply
  • 1006 views

I have a request-information component through which an user can submit his enquiry. When he submits the form, the thank-you page that I have created is shown to the user.

 

Now the scenario is such that say the thank-you page has an url: abc.com/xyz/thank-you.html But I don't wan't anyone and everyone to see the page when they simply hit the url instead they should see Server Error (500). The page should only come up when the user submits an enquiry.

 

How to stop the page from directly loading when I put in the url in the address bar?

 

@santoshsai  @debal_das @lukasz-m @arunpatidar @briankasingli

 

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 arunpatidar

Hi,

There are two ways to do it.

1. Client side - check if page has referer that means , the user is not hitting page directly, this can be done using javascript, If no referer then you redirect user to 404 page, put this in head section so no rendering is done.

document.referrer

2. From server side check the referer header , if null then set 404 or 400 but for this page should not be cached.

 String refererHeader = request.getHeader("referer");
if (refererHeader == null) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST); return; }

 

1 reply

arunpatidar
Community Advisor
arunpatidarCommunity AdvisorAccepted solution
Community Advisor
August 2, 2022

Hi,

There are two ways to do it.

1. Client side - check if page has referer that means , the user is not hitting page directly, this can be done using javascript, If no referer then you redirect user to 404 page, put this in head section so no rendering is done.

document.referrer

2. From server side check the referer header , if null then set 404 or 400 but for this page should not be cached.

 String refererHeader = request.getHeader("referer");
if (refererHeader == null) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST); return; }

 

Arun Patidar
ac6600Author
Level 2
August 2, 2022

Hi @arunpatidar ,

 

Since I am very new to AEM, can you help me out by mentioning where exactly I need the 

document.referrer

if I am going with the client-side approach. Will it be at the component level JS or page component js?

 

Thanks! 

arunpatidar
Community Advisor
Community Advisor
August 3, 2022

You can put this code in head.js, so that check can be done early as possible. 

 

Arun Patidar