Hi there,
I'm currently using AEM as a Cloud Service and have Akamai configured on top of it. Akamai is correctly routing traffic to my AEM publish instance at: "publish-pxxx-exxxx.adobeaemcloud.com" and everything seems to work fine at first glance. However, I'm encountering an issue with redirects defined in the Dispatcher configuration. When a redirect is triggered, the "Location" header returned includes the full origin domain instead of the CDN domain, which causes the user to be redirected away from the CDN.
For example, I have the following rule in my dispatcher:
RewriteRule ^/?$ /index [R=301,L]
The flow is as follows:
A user accesses: www.mycdn.domain/
Akamai forwards the request to my publish instance: publish-pxxx-exxxx.adobeaemcloud.com/
The Dispatcher processes the request and issues a 301 redirect
The response includes a Location header pointing to publish-pxxx-exxxx.adobeaemcloud.com/index
Instead, I would expect it to redirect to www.mycdn.domain/index
This behavior causes the user to leave the CDN domain, which is not ideal.
Am I missing something here? Is this the expected behavior when using Apache redirects behind a CDN? Any explanation and suggestions for how to handle this properly would be greatly appreciated.
Thanks in advance!
Solved! Go to Solution.
Views
Replies
Total Likes
I found the solution myself. @AmitVishwakarma was almost right — but the simplest approach turned out to be asking the CDN provider to rewrite the headers. That way, I didn't have to redo thousands of redirects.
Thanks for the support!
Views
Replies
Total Likes
Hi @New_Noob ,
You may try below rules
RewriteCond %{HTTP_HOST} ^(www\.mycdn\.domain)$
RewriteRule ^/?$ https://%1/index [R=301,L]
Thanks
Views
Replies
Total Likes
Than you for the reply, i think that would work, however, i am trying to understand if this behavior is expected, or if there is something wrongly configured in the CDN or Apache.
Updating the redirects as you suggested may work but i have thousands of redirects, so i am hesitant to update all of them.
Views
Replies
Total Likes
hi @New_Noob ,
Instead of rewrite rule,
You may cross check if server name and alias is correctly mentioned, if not try like below
e.g.
ServerAlias ${CH_HUB_VHOST}
<VirtualHost *:80>
ServerName publish
ServerAlias mycdn.domain.com
# other configuration settings
</VirtualHost>
Thanks
Views
Replies
Total Likes
Hi @New_Noob ,
You're seeing:
Location: https://publish-pxxx-exxxx.adobeaemcloud.com/index
You want:
Location: https://www.mycdn.domain/index
This happens because Apache/Dispatcher sees the origin host header, and uses that in generated redirects.
You must preserve the original host header (CDN domain) and dynamically build the redirect Location using that.
Use Apache RewriteCond %{HTTP_HOST} to preserve the incoming host in the Location header:
Dispatcher Rewrite Rule Fix:
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/?$ https://%{HTTP_HOST}/index [R=301,L]
This captures the original host from the Akamai request and uses it in the redirect, not the internal Adobe domain.
Why the Default Behavior Happens
The default RewriteRule ^/?$ /index [R=301,L] returns a relative redirect, and Apache (or the client) prepends the current host. Since Akamai forwards the request to publish-pxxx-exxxx.adobeaemcloud.com, that becomes the base.
Note:
If your CDN terminates HTTPS (which Akamai usually does), the redirect must be HTTPS in the Location header, or it will cause browser mixed-content issues.
Always use the fully qualified URL in redirects from Apache when behind a CDN.
You said you have thousands of redirects — instead of hardcoding them all with full URLs, use %{HTTP_HOST} dynamically everywhere:
Example:
RewriteRule ^/old-page$ https://%{HTTP_HOST}/new-page [R=301,L]
You can also wrap this with a RewriteMap if needed.
Regards,
Amit
Views
Replies
Total Likes
Than you for the explanation @AmitVishwakarma
I suppose I have no other option but to implement the redirects using the Host as you suggested. I had tried setting the Host header in the Akamai request to the origin (publish-xxx-xxx, which is behind Fastly), but Fastly responded with a 421 error.
Just to clarify, even though I can use ${HTTP_HOST}, I would still need to update all my existing redirects to include it, right?
Views
Replies
Total Likes
Hi @New_Noob ,
No, you do not need to manually update every single redirect rule.
You can centralize the fix using one of these 2 reliable methods depending on your setup.
Why This Happens
Apache’s RewriteRule creates redirects using:
- A relative path (/index) => Apache prepends the current host (e.g., publish-xxx).
- A fully qualified URL (https://%{HTTP_HOST}/index) => You control the domain in Location.
When you're behind CDNs like Akamai => Fastly => Adobe AEM, they pass their internal host to Apache. So unless you explicitly set the host using %{HTTP_HOST}, you get a Location header pointing to the origin, not your CDN domain.
Try below option:
Option 1: Add a Global Redirect Wrapper (Preferred for Many Rules)
If you already have lots of redirect rules like:
RewriteRule ^/old$ /new [R=301,L]
You don’t have to touch each one.
Instead, wrap your redirect logic in a global RewriteCond + RewriteRule` that prepends the host, like this:
# Ensure redirects use the original host
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
What this does:
- Prevents infinite loops with REDIRECT_STATUS
- Converts any relative redirect into an absolute one using %{HTTP_HOST} (e.g., your Akamai CDN domain)
- Keeps all your existing redirect rules unchanged
Option 2: Use mod_headers to Rewrite Location Header
If you want to be even cleaner and rewrite the Location header on all 301/302 responses, you can use:
Header edit Location ^https?://[^/]+ https://%{HTTP_HOST}e
Note:
- This requires mod_headers
- Works best when you're returning absolute URLs in redirects
- %{HTTP_HOST}e uses the value from the environment variable
- This is especially powerful if you can't control all rules but want a centralized fix.
Regards,
Amit
Views
Replies
Total Likes
Hi @New_Noob
Please check setup here https://medium.com/@toimrank/custom-domain-name-with-customer-managed-cdn-using-edge-key-aemaacs-c7e...
For other CDN settings
Views
Replies
Total Likes
Hi @New_Noob,
Did the shared solution help you out? Please let us know if you need more information. Otherwise kindly consider marking the most suitable answer as ‘correct’.
If you've discovered a solution yourself, we would appreciate it if you could share it with the community.
Views
Replies
Total Likes
I found the solution myself. @AmitVishwakarma was almost right — but the simplest approach turned out to be asking the CDN provider to rewrite the headers. That way, I didn't have to redo thousands of redirects.
Thanks for the support!
Views
Replies
Total Likes