Expand my Community achievements bar.

SOLVED

Extensionless URL

Avatar

Level 3

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  

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@nivethaS ,

This condition 

RewriteCond %{REQUEST_URI} !^/$

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

View solution in original post

5 Replies

Avatar

Level 3

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  

Avatar

Community Advisor

@nivethaS ,

 

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.

Avatar

Level 3

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

Avatar

Correct answer by
Community Advisor

@nivethaS ,

This condition 

RewriteCond %{REQUEST_URI} !^/$

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