Expand my Community achievements bar.

Radically easy to access on brand approved content for distribution and omnichannel performant delivery. AEM Assets Content Hub and Dynamic Media with OpenAPI capabilities is now GA.
SOLVED

AEM Dispatcher mod_rewrite - redirect if file does not exist

Avatar

Level 4

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

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

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

View solution in original post

2 Replies

Avatar

Community Advisor

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

Avatar

Correct answer by
Employee Advisor

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