Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

put links without file extension (.php)

Tags:

php

apache

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?

like image 481
Braggae Avatar asked Aug 02 '13 13:08

Braggae


People also ask

How do I remove page file extension from URL?

The . html extension can be easily removed by editing the . htaccess file.

Is it necessary for a file to have PHP extension to run?

If they are used trough include or require , any extension will work.

What are PHP file extensions?

php file extension refers to the name of a file with a PHP script or source code that has a ". PHP" extension at the end of it. It's similar to a Word file with a . doc file extension.


2 Answers

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"  
like image 165
JohnnyFaldo Avatar answered Oct 03 '22 02:10

JohnnyFaldo


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.

like image 41
Seth Battin Avatar answered Oct 03 '22 01:10

Seth Battin