1

Currently on my site I'm using statements like:

include 'head.php'; 
include 'footer.php';

i wanted to add the .php dynamically using the .htaccess, is that possible to do. SO that there is no need for me to add .php in code every time when where i add the php page using the include or require method

Expected output is

include 'head'; 
include 'footer'; 
1
  • I think this is not a big overhead, don't do something that not useful for code usability. Commented Apr 1, 2014 at 9:55

3 Answers 3

2

It is not possible man! If the goal is to avoid repeating .php every time, Consider creating a php function, such as my_include

   function my_include(filename){
        include filename.'.php';
   }

Then calls are

my_include('head');
my_include('foo');
my_include('bar');
my_include('footer');

EDITS: This function is useful, you can check whether if the file exists or not. This in order to avoid exception

   function my_include(filename){            
        include file_exists(filename.'.php')?filename.'.php':'404.php';
   }

And the 404.php may look like

  <?php
   echo 'some content were not found';
  ?>
Sign up to request clarification or add additional context in comments.

Comments

1

.htaccess files can be used to modify the behaviour of the web server which is hosting your PHP app. When an HTTP request comes in without the trailing .php, you can use htaccess to make the web server invoke the file with .php nonetheless. That's a feature supported by the web server. All this is irrelevant once PHP is invoked. include is a PHP statement which is evaluated by PHP according to PHP's rules. PHP doesn't know or care about .htaccess files. PHP requires you to provide the complete path to a file you want to include. What you want is not possible.

Comments

1

That is not possible with .htaccess.

You can rename them using your favorite IDE as this is not the job of .htaccess and it cannot read the inner logic of your PHP code.

Solution is replacing them using your PHP IDE , even a Notepad can help.

1 Comment

your are also correct i accepted the other answer because of his detailed explanation. Thanks for your answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.