Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache .htacces Rewrite Rule to Remove .php File Extensions

The title explains the basics, now, here comes the problem:

I have many files that look like this:

<?php include 'mynavbar.php';   
include 'myheader.php';    
include 'myfirstline.php'; 
include 'mybuttons.php';   
include 'myadvert.php'; 
include 'myimg.php';?>

And I can't edit all the files and remove the .php. How can I not show the .php in the address bar but still be able to include the pages by using the .php ending

like image 368
XCS Avatar asked May 19 '11 17:05

XCS


People also ask

How do I remove page file extension from URL?

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


1 Answers

Following code should work for you in .htaccess file to hide .php extension:

Options +FollowSymlinks -MultiViews
RewriteEngine on

# to make `/path/index.php` to /path/
RewriteCond %{THE_REQUEST} ^GET\s(.*/)index\.php [NC]
RewriteRule . %1 [NE,R=301,L]

RewriteCond %{THE_REQUEST} ^GET\s.+\.php [NC]
RewriteRule ^(.+)\.php$ /$1 [NE,R=301,L,NC]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

Also remember that these rules will have NO impact in your php includes eg: include 'myheader.php';

It is because those includes are processed by php itself and don't go through Apache.

like image 165
anubhava Avatar answered Sep 21 '22 16:09

anubhava