Expand my Community achievements bar.

301 Redirect not working on Cloud

Avatar

Level 6

Hello Community,

 

Using AEM as Cloud, there are two versions for customer registration with following URLs https://domain/abc and https://domain/xyz. We don't want https://domain/xyz anymore and want to have a 301 redirect to /abc page. 

 

I have a rewrite now RewriteRule ^/xyz\/?(.*)$ /abc [R=301, L] this is working but partially. 

 

When I hit this URL on QA environment https://qa.myproject.com/xyz , it is redirected to https://publish-domain/abc, page is served from AEM publisher. Expected redirection should be https://qa.myproject.com/abc

 

M I missing anything here ? I have also added sling mapping for https://qa.myproject.com domain under /conf/global/mappings but no luck.

 

Thanks in advance

12 Replies

Avatar

Community Advisor

Hi @Love_Sharma,

 

Try to add a host and a protocol to your rule:

RewriteRule ^/xyz\/?(.*)$ ${PROTOCOL}://%{HTTP_HOST}/abc [R=301,L]


In addition, consider implementation of https://experienceleague.adobe.com/en/docs/experience-manager-cloud-service/content/implementing/con... on your project.

 

Best regards,

Kostiantyn Diachenko.

Kostiantyn Diachenko



Check out AEM VLT Intellij plugin


@konstantyn_diachenko I already tried this approach. This redirect me to ${http_host}/abc, not resolving https_host.

Avatar

Community Advisor

Hi @Love_Sharma ,

 

I suggested to use %{HTTP_HOST}, not a ${HTTP_HOST}. The HTTP_HOST is a Apache variable. See that here: https://httpd.apache.org/docs/2.4/expr.html#vars.

 

Best regards,

Kostiantyn Diachenko.

Kostiantyn Diachenko



Check out AEM VLT Intellij plugin


@konstantyn_diachenko I still see it's not working. In rewrite rule file I have two rules in below sequence, is that something impacting

RewriteRule ^/abc\/?(.*)$ /content/us/en/customer-registration.html [NC, QSA, PT, L]

RewriteRule ^/xyz\/?(.*)$ https:://%{HTTP_HOST}/abc [R=301,L]

Avatar

Community Advisor

Hi @Love_Sharma ,

 

Your rules are not valid and probably your dispatcher doesn't accept these rules. I wonder how were they deployed.

 

Mistakes:
- Apache rule flags must not have whitespaces after commas

- You have 2 colons after https

 

Fixed version:

RewriteRule ^/abc\/?(.*)$ /content/us/en/customer-registration.html [NC,QSA,PT,L]

RewriteRule ^/xyz\/?(.*)$ https://%{HTTP_HOST}/abc [R=301]

 

Try to deploy these changes.

 

Best regards,

Kostiantyn Diachenko.

Kostiantyn Diachenko



Check out AEM VLT Intellij plugin


@konstantyn_diachenko Those are editor copy/paste issue while pasting here. There are no gaps and double colon in the code

Avatar

Community Advisor

Hi @Love_Sharma ,

 

I realized that we have a lack of details. We are just guessing. Please, provide information in next format:
1) What request do you do

2) What rule do you expect to handle it

3) What result do you anticipate

4) What result do you actually have

 

Best regards,

Kostiantyn Diachenko.

Kostiantyn Diachenko



Check out AEM VLT Intellij plugin


Avatar

Level 5

Hi @Love_Sharma ,

I believe the following rewrite rule should be enough:


RewriteEngine On
RewriteRule ^/xyz/?$ /abc [R=301,L]


This rule will work across all domains and redirect /xyz to /abc with a 301 status. It’s sufficient for this type of redirection and doesn't require relying on Sling Mappings.

 

Thanks.

 

Thanks.

Avatar

Community Advisor

Hi @Love_Sharma ,

301 redirect from /xyz to /abc on AEM as a Cloud Service, ensuring it respects the domain (qa.myproject.com or otherwise) and avoids redirecting to the internal publish-domain, follow these specific steps:

1. Use Apache Rewrite Rule with Correct Host Preservation

In your Apache configuration (typically in your vhost or dispatcher rules):

RewriteEngine On

# Match /xyz or /xyz/ and redirect with preserved domain and protocol
RewriteRule ^/xyz/?$ https://%{HTTP_HOST}/abc [R=301,L]

This will:

  - Match /xyz or /xyz/

  - Redirect to the same host (e.g., qa.myproject.com)

  - Use https explicitly (important for Cloud)

  - Ensure the redirect is a 301 (permanent)

2. Optional – Force Protocol to HTTPS (Recommended)

If your domain might be hit over HTTP and you want to force HTTPS:

RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Place this above your /xyz rule if needed.

3. Do NOT use Sling Mapping for Simple Redirects

You do not need Sling mappings (/conf/global/mappings) for simple path-to-path redirects. These are mostly used for vanity URLs and internal resource resolution.

Final Dispatcher Snippet (Minimal Example)

RewriteEngine On

# Optional: Enforce HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Redirect /xyz to /abc
RewriteRule ^/xyz/?$ https://%{HTTP_HOST}/abc [R=301,L]

If you're using AEM Cloud Dispatcher SDK, add this inside your custom rewrite.rules in conf.d/rewrites.

Regards,
Amit

Avatar

Level 6

@AmitVishwakarma Thanks for the reply. Rule is still not working. Not sure if below sequence could be an issue

RewriteRule ^/abc\/?(.*)$ /content/us/en/customer-registration.html [NC, QSA, PT, L]

RewriteRule ^/xyz\/?(.*)$ https:://%{HTTP_HOST}/abc [R=301,L]

Avatar

Employee

AEM documentation and troubleshooting refer specifically to correct handling of virtual hosts and hostname/redirect processing:

Solution

Use a relative URL in your redirect

This is what you're attempting, and should work:

RewriteRule ^/xyz/?$ /abc [R=301,L]

or

RewriteRule ^/xyz(/.*)?$ /abc [R=301,L]
  • By specifying a relative URL (/abc), the client’s browser should stay on the same domain, using the Host header of the original request.

Check for RewriteBase and VirtualHost config

  • Make sure you are running this rule inside the correct <VirtualHost> block for qa.myproject.com.
  • If you are, and still getting a redirect to https://publish-domain/abc, there may be another RewriteRule firing first, or your dispatcher farm/vhost (or AEM itself) is configured to externalize/resolve using the publisher’s DNS name.
  • Try enabling RewriteLog to trace what rule is being executed.

Avoid hardcoded domains

  • Never use absolute URLs with internal domains in the rewrite/redirect rules.
  • Always use relative paths only unless you want to specifically redirect to another domain.

Check /externalizer config in AEM

  • If you use AEM's Externalizer service in custom code or config, ensure the publish environment is registered with the correct external/public base URL, not the internal publish hostname.

Sling Mappings

  • Sling mappings are not usually involved for this particular redirect. If you do have custom mappings, make sure none are producing absolute URLs with internal hostnames for /abc.

TL;DR

  • Keep your redirect rule as:
    RewriteRule ^/xyz/?$ /abc [R=301,L]
  • Check that this is inside the VirtualHost config for your public domain.
  • Make sure there’s no earlier rule or AEM config causing an absolute URL with the internal publish host.
  • You do NOT need a full absolute URL for the redirect target, unless you are explicitly redirecting across domains.

References:

Avatar

Administrator

@Love_Sharma 

Just checking in — were you able to resolve your issue?
We’d love to hear how things worked out. If the suggestions above helped, marking a response as correct can guide others with similar questions. And if you found another solution, feel free to share it — your insights could really benefit the community. Thanks again for being part of the conversation!



Kautuk Sahni