6

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

3 Answers 3

12

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.

Sign up to request clarification or add additional context in comments.

8 Comments

I've tried like 10 rewrite rules and none of them worked. This one works perfect. Thanks a lot!
If you are kind, can you tell me how can I also remove the "/index" for the homepage? ;;)
About could I also remove the "/index" for the homepage Sure I will try to put that rule condition also. Will update the answer in a short while.
@Cristy: Edited my answer above to take care of removal of /index pls verify.
This works wonderfully, except in the case of navigating to /foo/ - The trailing slash makes it error trying to look for foo/.php Anyway to make this work? Thanks!
|
4

The following RewriteRules will assume the PHP extension for any file that is not otherwise matched. It should go at the bottom of your RewriteRules in your .htaccess file.

# Force PHP extension if not a directory
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^(.*)$ - [L]

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

This is for URL Rewriting. As far as include statements, you still need the PHP extension as these are physical file paths. If you need to so something fancy there, you should look into symbolic links.

Comments

0

You are not taking the correct path for fixing the problem you trying to solve.

If I understood well, what you want to do is having an url like :

http://example.com/my/url

instead of (for example):

http://example.com/my/url.php

If this is the case, what you should look for is rewrite url

7 Comments

This is a very low quality answer. You are not adding anything towards the community.
Thank you for poiting that out. I should have copied something that exists already 200^2000 times on the web just to have something more useful than an url to share. Because I'm sure you believe my answer is incorrect? Good boy ...
I appreciate your comment, but I don't think it's useful to change my comment : to what? If I edit it to insert the solution, I will just copy the accepted answer. The accepted answer is complete for his request, mine was more generalist, because at the time, I believed the question needed a better generalist answer (via google) than a specific one. I thought the user asking the question was starting with .htaccess/url-rewriting and redirecting him on more links than just his specific answer was more helpful. That's why.
I accept your downvote because 1. you presented yourself as the downvoter (which is a great step) 2. You explained why. Most of the people who downvote don't take the time to explain why. I believe it's when you downvote something that you need, more than ever, to explain why, because you help the author understand his errors (or why you disagree with him).
I appreciate your comments. Here's some upvote for your comments atleast :-)
|

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.