Expand my Community achievements bar.

Submissions are now open for the 2026 Adobe Experience Maker Awards.
SOLVED

Help Needed with Apache Rewrite Rules for .html Check and Redirection

Avatar

Level 2

Hi everyone,

I'm currently working on a project where I need to set up some rewrite rules. The goal is to check if a requested page has a .html extension. If it doesn't, the request should be redirected to a 404.html page. Here's what I have so far:

RewriteCond "%{REQUEST_URI}" "^/content/project/(.*)"
RewriteRule "/(.*)" "/%1" [L,R=301,NE]
RewriteRule "/(.*)(.html)$" "/content/project/$1.html" [PT,NE]


I'm looking for advice on how to modify these rules to achieve the desired functionality, without breaking any existing functionality. Any suggestions or improvements would be greatly appreciated!

Thanks in advance for your help!

1 Accepted Solution

Avatar

Correct answer by
Employee

Recommended Rewrite Rule

RewriteEngine On

# Allow requests that already end with .html (let them pass through)
RewriteCond %{REQUEST_URI} \.html$ [NC]
RewriteRule ^ - [L]

# Allow requests for required static files (optional, e.g. assets, clientlibs, etc.)
RewriteCond %{REQUEST_URI} \.(css|js|png|jpg|jpeg|gif|ico|svg|woff|woff2|mp4|pdf|xml)$ [NC]
RewriteRule ^ - [L]

# If it’s anything else, redirect to /404.html
RewriteRule ^ /404.html [R=302,L]

Explanation:

  • The first rule says: if the request ends with .html, do nothing (let it pass through).
  • The next allows asset requests (static files) to pass through. Adjust the extensions as needed for your project.
  • Any other request gets redirected to /404.html using a soft (302) redirect (use [R=301,L] for a permanent redirect).

Why this approach:

  • It’s simple and robust.
  • It doesn’t block static resources or cause infinite redirects if someone visits /404.html directly.
  • Avoids accidentally blocking clientlibs or embedded images/scripts/styles.

Notes:

  • If your 404 page is not at the server root, adjust /404.html to the correct path.
  • Don't use [PT] unless you need further processing by mod_proxy or mod_alias.

References:

 


If you want to specifically handle only pages under /content/project/:

  • Adjust the conditions to match that path.
  • Example below:
RewriteCond %{REQUEST_URI} ^/content/project/
RewriteCond %{REQUEST_URI} !\.html$ [NC]
RewriteRule ^ /404.html [R=302,L] 

 

View solution in original post

7 Replies

Avatar

Level 5

Hi @sai_charanAr ,

Try below rules - 

# Check if request does NOT end in .html
RewriteCond %{REQUEST_URI} !\.html$ [NC]


# Redirect such requests to 404.html
RewriteRule ^/content/project/.*$ /content/project/404.html [L,R=302]

 

# If request ends in .html, rewrite it properly
RewriteRule ^/?(.*)\.html$ /content/project/$1.html [PT,L,NE]

Let me know if it works.
Thanks.

Avatar

Level 2

Hi @ShivamKumar  , i have tried this rule but for some of hte pages if we access like   www.example.com/newblogs 

which should redirecdt to 404.html because is not existing in the content
but im getting the below page as result and the below are current etc mapping for hte project
sling:internalRedirect :/content/project/(.*).html
sling:match :www.example.com/$1.html

sai_charanAr_0-1745580546818.png

 

Avatar

Level 5

 

Hi @sai_charanAr ,

 

The current sling:internalRedirect and sling:match rules are causing unexpected redirects. The sling:internalRedirect rule is redirecting any request to /content/project/(.*).html, even if the resource doesn't exist, and the sling:match rule appends .html to the incoming URL. This results in redirects to non-existent pages instead of the expected 404. To fix this, you need to ensure that the page exists before redirecting. If it doesn't exist, modify the rules to explicitly redirect to 404.html.


Hope this helps.

Avatar

Community Advisor

Hi @sai_charanAr,

Try this

# Step 1: Check if the request is for /content/project/*
RewriteCond %{REQUEST_URI} ^/content/project/.*$

# Step 2: If it ends with .html, allow normal flow (nothing special needed)
RewriteCond %{REQUEST_URI} \.html$ [NC]
RewriteRule ^ - [L]

# Step 3: If it does NOT end with .html => redirect to /404.html
RewriteCond %{REQUEST_URI} !\.html$ [NC]
RewriteRule ^ /404.html [L,R=302]

# Step 4: Rewrite nice URLs to internal content paths if needed
RewriteRule ^(.*)\.html$ /content/project/$1.html [PT,L]

Hope that helps!


Santosh Sai

AEM BlogsLinkedIn


Avatar

Level 2

Hi @SantoshSai  , thanks for your response , i have tried the similar rules it was working only if the 404.html redirection is R=301 ,Actually we need the 404 redirection as passthrough the url should not change in that case its not redirecting to 404 page in passthrough throwing  the page not found (in the server ) error as mention in the above image.

Avatar

Correct answer by
Employee

Recommended Rewrite Rule

RewriteEngine On

# Allow requests that already end with .html (let them pass through)
RewriteCond %{REQUEST_URI} \.html$ [NC]
RewriteRule ^ - [L]

# Allow requests for required static files (optional, e.g. assets, clientlibs, etc.)
RewriteCond %{REQUEST_URI} \.(css|js|png|jpg|jpeg|gif|ico|svg|woff|woff2|mp4|pdf|xml)$ [NC]
RewriteRule ^ - [L]

# If it’s anything else, redirect to /404.html
RewriteRule ^ /404.html [R=302,L]

Explanation:

  • The first rule says: if the request ends with .html, do nothing (let it pass through).
  • The next allows asset requests (static files) to pass through. Adjust the extensions as needed for your project.
  • Any other request gets redirected to /404.html using a soft (302) redirect (use [R=301,L] for a permanent redirect).

Why this approach:

  • It’s simple and robust.
  • It doesn’t block static resources or cause infinite redirects if someone visits /404.html directly.
  • Avoids accidentally blocking clientlibs or embedded images/scripts/styles.

Notes:

  • If your 404 page is not at the server root, adjust /404.html to the correct path.
  • Don't use [PT] unless you need further processing by mod_proxy or mod_alias.

References:

 


If you want to specifically handle only pages under /content/project/:

  • Adjust the conditions to match that path.
  • Example below:
RewriteCond %{REQUEST_URI} ^/content/project/
RewriteCond %{REQUEST_URI} !\.html$ [NC]
RewriteRule ^ /404.html [R=302,L] 

 

Avatar

Level 2

Hi @rks1108 ,

Thank you so much for your response and for trying to help. The requirement I have is to make the 404 page a passthrough, so the user does not experience a URL change.

I appreciate your assistance!