AEM Dispatcher mod_rewrite - redirect if file does not exist | Community
Skip to main content
brendanf9753525
Level 4
September 11, 2018
Solved

AEM Dispatcher mod_rewrite - redirect if file does not exist

  • September 11, 2018
  • 2 replies
  • 2847 views

Hey all,

I'm attempting to write an apache mod_rewrite statement on my dispatcher that will redirect a URL only if the file does not exist/hasn't been published yet.

For example, if I have a page that does not exist named

www.testsite.com/dir1/non-existing-page-123

and one that does exist named

www.testsite.com/dir1/existing-page-123

The dispatcher should check if the file exists and redirect the non-existing-page-123, but serve the existing-page-123. For clarity - this is from an old content migration, now is a mix of old + new content URLs.

I wrote the mod_rewrite statement below that checks for the file to not exist, but I realized that the file will only exist on the dispatcher if it is cached and the dispatcher should be checking the publisher for the existence of the page. This below statement redirects everything with a -123 at the end of it to the /dir 1 page, existing or not.

RewriteCond ^dir1/(.+)-123 -!-f

RewriteRule ^dir1/(.+)-123               /dir1            [L,R=301]

I have other rewrite conditions and rules that are working okay, but this is my first one dealing with a conditional to check for a file.

Is there a way in apache mod_rewrite on the dispatcher to check if a file has been published from AEM?

Thanks

Brendan

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 joerghoh

Your approach does not work. Because if the file is not in the dispatcher cache, the dispatcher will reach out to AEM to fetch that file. If AEM returns a 404, the dispatcher will delegate the remaining part of the request handling to the apache httpd. And on httpd side the standard error handling kicks in.

That mans, that you should configure the 404 errorhandler. I don't think that you can achieve the behavior you are looking for with mod_rewrite.

Jörg

2 replies

arunpatidar
Community Advisor
Community Advisor
September 12, 2018

Hi,

Not sure if you can know the state of file if it is published or not from dispatcher.

But if you know the old and new urls for a page you can setup vanity urls to redirect user to new page if they try to access page from old urls and for non-existing pages from 404 redirect to old pages or can be accessed directly.

Arun Patidar
joerghoh
Adobe Employee
joerghohAdobe EmployeeAccepted solution
Adobe Employee
September 12, 2018

Your approach does not work. Because if the file is not in the dispatcher cache, the dispatcher will reach out to AEM to fetch that file. If AEM returns a 404, the dispatcher will delegate the remaining part of the request handling to the apache httpd. And on httpd side the standard error handling kicks in.

That mans, that you should configure the 404 errorhandler. I don't think that you can achieve the behavior you are looking for with mod_rewrite.

Jörg