1

I would like to remove the .php extension from my website.

I have this structure: /var/www/site/

FYI, in folder site, I have a lot of subfolders.

In site folder, I create .htaccess file. I inputed these line of code:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
</IfModule>

After this change the website still require .php extension. So I tried to modify Apache2.conf and to add:

    <Directory /var/www/site>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
    </Directory>

I restarted the server with:

sudo service apache2 restart

After that, the website is still requiring the .php extension on localhost.

Any idea what am I doing wrong? Is there other solutions?

Steph
  • 31
  • 2
  • 6
  • You could remove your `.htaccess` file entirely and simply add `MultiViews` to the `Options` declaration in your `Apache2.conf` – Phil Jan 18 '18 at 23:16
  • I will take a look at this answer and see if it fixes my issue. – Steph Jan 18 '18 at 23:16
  • Options Multiviews is working. Thanks. So why do people use .htaccess to remove the .php extension? It looks more complicated, no? – Steph Jan 18 '18 at 23:20
  • Lack of awareness I'd imagine. There's a lot of tutorials and examples for mod-rewrite. `MultiViews` would run in to trouble if you had directories or multiple files with the same base-name too, eg `index.html`, `index.php`, `index.jpg` – Phil Jan 18 '18 at 23:28

1 Answers1

0

Try in this way:

# Remove .php extension
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^\.]+)$ $1.php [NC,L]
</IfModule>
benny-ben
  • 422
  • 5
  • 15