Apache.htacces Rewrite Rule to Remove.php File Extensions.

The name explains the basics, now the problem is:

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 cannot edit all files and delete .php . How can I not show .php in the address bar but still be able to include pages using .php end

+6
source share
3 answers

The following code should work for you in the .htaccess file to hide the .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 a NO effect in your php, for example: include 'myheader.php';

This is because these included processes are handled by php itself and do not go through Apache.

+11
source

The following RewriteRules will use the PHP extension for any file that does not otherwise map. It should go to the bottom of the 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 rewriting the url. Regarding include statements, you still need the PHP extension, as these are physical file paths. If you need something here, you should study symbolic links.

+4
source

You are not taking the right path to resolve the problem you are trying to solve.

If I understood correctly, then what you want to do is to have a URL:

 http://example.com/my/url 

instead (for example):

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

If so, then you should look for a rewrite URL

-1
source

Source: https://habr.com/ru/post/888566/


All Articles