I have this website where when I want to go from index.php to leistungen.php the url should be …website/leistungen. The problem is I can’t get the .php away. The website is being hosted on Hostinger.
To achieve this I tried several different ways on how the RewriteRule is written. The only thing that seems to work partly is the following:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*).php$ /$1 [L,R=301]
I also tried but it didn’t work:
Rewrite Engine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.php [NC,L]
Just to name a few. Sadly I haven’t saved the code which I tried out.
I say partly because now you can manually access …website/leistungen but if you click on a href redirecting to the site it always goes to …website/leistungen.php.
Different ways for the href’s have been:
leistungen
, /leistungen
, ...website/leistungen
There is one way to make it work but it is kind of dirty in my eyes. On the Hostinger website I can create a redirect rule from …website/leistungen.php to …website/leistungen. But as I said before I don’t want to do it this way as it would mean that I have to input every website for http and https manualy and this is just too much work.
Instructions which I tried:
StackOverflow: url-rewrite-not-working-on-htaccess
Alexican: how-to-remove-php-extensions-with-htaccess
Tried to solve the error with the help of perplexity.ai
Had extensive discussions with the Support from Hostinger.
M. Carle is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.