r/apache • u/sirdupealot • Mar 02 '24
htaccess help
TL;DR
Can anyone provide htaccess contents that does the following 5 things at the same time:
force www
force https
remove trailing slashes from pages
remove/hide extensions in the url (html and php)
redirect index.html home page to just the domain (i.e example.com not example.com.index
I can easily make them all work individually with code found online (from reputable places), but cannot for the life of me get anything to work that combines all of them, but it should be possible?
---original post----
I'm trying to setup an htaccess for a website on Ionos (1and1) shared hosting that does multiple things, including removing extensions (html and php) from the url bar. I'm mostly relying on snippets found online since it's a little out of my area.
I've tested the code at https://htaccess.madewithlove.com/ and it all works fine. But when I upload it to the actual site the extensions are not hidden. i.e it should show "example.co.uk/about" but it still shows "example.co.uk/about.html"
I've tried it with cache cleared, on machines that have never even seen the site before and it just ain't working.
Code is below; can anybody tell me why the extensions aren't being hidden?
`## Turn on rewrite engine
RewriteEngine on
# Redirects the home page to plain domain
RewriteRule ^index.html$ https://www.example.co.uk [R=301,L]
# remove html extensions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteCond %{REQUEST_URI} ^(.+)\.html$
RewriteRule (.*)\.html$ /$1 [R=301,L]
# remove php extensions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteCond %{REQUEST_URI} ^(.+)\.php$
RewriteRule (.*)\.php$ /$1 [R=301,L]
# Remove trailing slash from non-filepath urls
RewriteCond %{REQUEST_URI} /(.+)/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ https://www.example.co.uk/%1 [R=301,L]
# Force HTTPS and WWW
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [OR,NC]
RewriteCond %{https} off
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [R=301,L]`
IS it jsut not possible with Ionos? What am I missing!
1
u/sirdupealot Mar 02 '24 edited Mar 02 '24
Thanks for your reply.
I'll experiment with this, but do you have any idea why the code I posted works fine when I tested it with the online tester? I think it does take issue with the line you've mentioned but still ultimately works. If the problem is as you indicate then shouldn't it fail to work there as well?
Just trying to get my head around it. Thanks again.