Remove .html extension from url with htaccess

Hey Kirby Lovers,

if I have a running kirby site every page is available as

https://www.awesome-kirbypage.com/mypage

and

https://www.awesome-kirbypage.com/mypage.html

I searched the web for a simple htaccess solution to redirect to the version without .html or shorten the url by that. I found like a dozen different solutions, but none will work.

Has anyone a good setup for that and kirby?

Thanks!

Where does that extension come from? Kirby doesn’t produce URLs ending with .html…

Hm, I just noticed that you can in fact add .html to an URL and it still works, never realized that before.

I know, that’s not a direct problem of kirby…

but if you type it inside the url field of the browser or if you are following a link with that extension it displays the page with the trailing .html

Example: https://getkirby.com/docs/guide.html

NOTE: When testing your .htaccess do not use 301 redirects. Use 302 until finished testing, as the browser will cache 301s. See apache - Tips for debugging .htaccess rewrite rules - Stack Overflow

Thanks… that note helped… the reason why none of my initial solutions worked resulted from caching.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /$1 [L,R=301]

This did the job. Thanks again :smiley:

Because 301 redirects are cached by the browser, think twice before using them (in case you might need these URLs in the future).

1 Like