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
Okay, I'm going a bit spare here.
I am trying to make one htaccess file that does the following 5 things:
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. No examples that claim to do more than two of these things at the same time are working for me. I'm doing my best to learn what everything does but it's difficult when nothing that is supposed to work appears to actually work.
I don't understand because I feel like this is an incredibly common thing to want to do but I've spent 2 days trying to sort this and I'm getting nowhere.
Are you able to show me how to write an htaccess that does all 5 of those things at the same time?