Michael Moor
Michael Moor

Reputation: 21

Hide file extension (.php) in .htaccess file

I know there are many topics. I tried them many times, but it doesn't work.

What do I want?

Instead of example.com/en/file.php, users see only example.com/en/file.

My .htaccess file:

DirectoryIndex index.php index.html

RewriteEngine on
*RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ /$1.php*


ErrorDocument 404 /index.php

RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^ru\/index\.php$ "http\:\/\/example\.com\/ru\/" [R=301,L]

RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^en\/index\.php$ "http\:\/\/example\.com\/en\/" [R=301,L]

etc. for every language

  1. What do I have to add to hide extensions?

  2. After it works, should I use links between pages as "file.php" or only "file"?

Upvotes: 0

Views: 802

Answers (3)

Michael Moor
Michael Moor

Reputation: 21

Fixed by adding one line:

RewriteEngine on
**Options -Multiviews**
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$      $1.php     [L,QSA]

Upvotes: 0

Marcelo Pascual
Marcelo Pascual

Reputation: 820

DirectoryIndex index.php index.html    

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$      $1.php     [L,QSA]

In your links, don't use extensions.

Upvotes: 0

showdev
showdev

Reputation: 29188

Perhaps something like this. Modify as you see fit for more specific pattern matching.

RewriteRule ^(?:(.*)/)?(.*)$ $2.php?lang=$1

Rewrites:

/en/fun => /fun.php?lang=en

/ru/fun => /fun.php?lang=ru

/fun => /fun.php?lang=

Upvotes: 1

Related Questions