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!
Solved! Go to Solution.
Views
Replies
Total Likes
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:
Why this approach:
Notes:
References:
If you want to specifically handle only pages under /content/project/:
RewriteCond %{REQUEST_URI} ^/content/project/ RewriteCond %{REQUEST_URI} !\.html$ [NC] RewriteRule ^ /404.html [R=302,L]
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
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
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
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!
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.
Views
Replies
Total Likes
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:
Why this approach:
Notes:
References:
If you want to specifically handle only pages under /content/project/:
RewriteCond %{REQUEST_URI} ^/content/project/ RewriteCond %{REQUEST_URI} !\.html$ [NC] RewriteRule ^ /404.html [R=302,L]
Views
Replies
Total Likes
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!
Views
Replies
Total Likes
Views
Likes
Replies