Rewrite engine causes huge error

Hey,
important is, that when I write some rewrite conditions in my .htaccess it outputs this:


After some research, I found out the reason. This is a code that removes the PHP extension.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

The problem is the first line, but when I delete it, the page will work, but the whole code won’t. I’m not sure if the code is fine, but the RewriteEngine seems to be a problem. Are there any solutions out there?

Final Code
php_value display_errors On
php_value mbstring.http_input auto
php_value date.timezone America/New_York

DirectoryIndex router.php

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

This will work:

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]

This also will work in sub-directories without pointing RewriteBase to them

There is a far easier way to disable file extensions on this hosting by just using:

Options +MultiViews
2 Likes

So you mean that the first line shouldn’t be there at all, and the code I’ve been using is just written wrong, right? That would make it clear a lot.

That’s a very helpful and short code! It works just amazing. Could I please ask where did you find this solution @TigerMANEK426?
I’m also looking for a non-www force and file blocking, that isn’t working for me either.

non-www force
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
file blocking

RewriteCond %{HTTP_REFERER} https://%{HTTP_HOST}%{REQUEST_URI} [NC]
RewriteRule .(gif|jpg|png|css|svg)$ https://domain.name/errors/403/ [R,L]

Try this

image

I found the code on Google.

When say “non-www force”, do you mean its the users choice to have www. at the start or not?

1 Like

I mean to block www at the start. So if you go to www.domain.net it will redirect you to https://domain.net

Ah, okay. Use this code:

RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
1 Like

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.