.htaccess; no extension required

I need .htaccess code that will process files without extension as PHP files.

Suppose visitors visit www.mywebsite.com/dir1/dir2/file.name , first it will look for the file dir1/dir2/file.name , and if it does not exist, it will look for dir1/dir2/file.name.php (so with the extension .php ).

Is this possible somehow?

+6
source share
3 answers

You can write a rewrite rule that takes effect only if the requested file does not exist.

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

You should take a look at the Apache Options directive, specifically http://httpd.apache.org/docs/2.0/content-negotiation.html , which you can include in your .htaccess file and will do just that.

Add to your .htaccess :

 Options +MultiViews 
+3
source

Well, the answer was a combination of answers. I needed to add Options +MultiViews to my Apache configuration. Then I extend Andrews answer to the following:

 RewriteEngine On RewriteCond %{REQUEST_URI} !^/ RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ $1.php [L] 

Regards and many thanks! Kevin

+1
source

Source: https://habr.com/ru/post/891569/


All Articles