Allow page URLs with or without trailing .html
I use different programs to keep track of the different parts of this website. MODx content management system (CMS) runs the front page and the project and about sections; WordPress organises the blog posts; OpenCart tends to the shop, and ikiwiki will compile the wiki. When I first installed MODx, I decided to give every page a .html
suffix as if they were static files.
When the other programs all shunned a suffix for their URLs, those used by MODx looked out-of-place. So I wanted to remove the suffix, but without breaking any incoming links to the pages. (Any links ending in .html
would return page not found, even if the rest of the link was correct). To do this, I needed a redirect to allow, but not require, the .html
suffix.
In .htaccess:
# Rewrite to allow html extensions on suffixless page urls
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*).html$ $1 [NC,L]
This could be further extended to allow any of a list of extensions:
# Allow whitelisted extensions to resolve to suffix-free url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*).(html|htm|php|aspx|net)$ $1 [NC,L]
Or alterred to work in reverse, adding .html
to a non-suffixed url to address a file:
# Add .html suffix if not present
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [L]
Sources:
sicanstudios.com > How to Remove php, html & htm Extensions with .htaccess css-tricks.com > Snippets > .htaccess > Remove File Extention from URLs