29

Is it possible to configure Apache in order not to show a file extension?

For example: Say I have domain.com/page.php but want to have domain.com/page as the url.

Any Ideas?

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Braggae
  • 841
  • 4
  • 13
  • 29

5 Answers5

54

Put this is your .htaccess file

#turn on url rewriting 
RewriteEngine on

#remove the need for .php extention 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

This allows you to access .php files without the extension, so your links should read

href="/somepage"

and this will direct to

href="/somepage.php" 
JohnnyFaldo
  • 4,121
  • 4
  • 19
  • 29
15

There is also MultiViews as a vhost or .htaccess configuration option. See http://httpd.apache.org/docs/2.2/content-negotiation.html#multiviews

From that page:

The effect of MultiViews is as follows: if the server receives a request for /some/dir/foo, if /some/dir has MultiViews enabled, and /some/dir/foo does not exist, then the server reads the directory looking for files named foo.*, and effectively fakes up a type map which names all those files, assigning them the same media types and content-encodings it would have if the client had asked for one of them by name. It then chooses the best match to the client's requirements.

Seth Battin
  • 2,811
  • 22
  • 36
  • 3
    Multiviews can be quite annoying, and get in the way of normal mod_rewrite rules if the choice of filenames and rewrite patterns overlaps. – David Goodwin Jun 11 '15 at 14:17
  • Very true. Use with caution. – Seth Battin Jun 11 '15 at 16:20
  • 1
    I added MultiViews in my vhost suffered for 2 days. I was unable to figure out why my clean get request url was not working.... Thanks @TheGingerDog – Indra Kumar S Sep 16 '16 at 08:46
  • 1
    The comment from @DavidGoodwin and this answer really helped solve a particularly nasty and hard to track conflict between rewrite rules and the MultiViews option enabled. – Scorpius Feb 21 '18 at 18:29
7

This is called URL rewriting. I had to use it for the first time recently and i used this tutorial to learn it, hope you'll find it great too :

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
Pascamel
  • 953
  • 6
  • 18
  • 28
4

Put this is your .htaccess file

RewriteEngine On
RewriteRule ^page?$ page.php

<a href="page">page</a>
3

You want a web server feature called URL rewriting - every web server application (apache, IIS, nginx) supports this. As the name suggests, it takes the requested URL and rewrites it into a specific format that you define.

There are many guides available on the www, even if you are using shared hosting solution you can still add/modify the .htaccess file to do this.

robert_murray
  • 2,070
  • 17
  • 9