Extensionless URL | Community
Skip to main content
Level 3
December 12, 2023
Solved

Extensionless URL

  • December 12, 2023
  • 1 reply
  • 1441 views

Hi ,

How to add .html to extensionless URL in rewrite rules.

When I tried the below code it is appending the .html but unfortunately the domain or language page is also getting affected by the same.

# Add .html for extensionless url's
RewriteCond %{REQUEST_URI} !\.html$
RewriteRule ^/(.*)$ /$1.html [R=301,L]

 

For example : In the below example .html shouldn't get added to the 1 & 2 but it should append to 3 & 4

1. https://www.example.com

2. https://www.example.com/en_US

3. https://www.example.com/en_US/test

4. https://www.example.com/en_US/test/testingPage  

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Sudheer_Sundalam

Hi @sudheer_sundalam 

Thanks this works but it is adding .html to the  https://www.example.com 

Is it possible to ignore this, please advice


@helptech ,

This condition 

RewriteCond %{REQUEST_URI} !^/$

in the rule should already handle that. Do you have any other rewrite rules that may be re-processing?

1 reply

arunpatidar
Community Advisor
Community Advisor
December 12, 2023
HelpTechAuthor
Level 3
December 12, 2023

Hi @arunpatidar 

 

I already gone through the same but here my condition is when user hits the page without .html it should append .html. In that case 

#Replace the .html with /
RewriteCond %{REQUEST_URI} \.html$
RewriteRule ^/(.*).html$ /$1/ [R=301,L,QSA]

Will this line work? Because we will have URL like 

3. https://www.example.com/en_US/test

4. https://www.example.com/en_US/test/testingPage  

Sudheer_Sundalam
Community Advisor
Community Advisor
December 12, 2023

@helptech ,

 

Try this,

# Rewrite to add html extension if a url does not end with a known extension
RewriteCond %{REQUEST_URI} !^/[a-zA-Z0-9_-]+$ [NC]
RewriteCond %{REQUEST_URI} !\.(html|pdf|xml|json|txt|ico|php|png|jpg|gif|aspx|css|js)$
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^(.*)$ $1.html

 You don't need do the 301 or any other redirect in case of appending .html.