Remove .php extension using PHP

I could help here. I use this to fix my URL, but I cannot figure out how to remove the .php extension. Now the url looks like this: http://mydomain.com/page.php/foo/123/bar/456

function decode_URL_parameters() {
   $path = @$_SERVER['PATH_INFO'];
   $url_array=explode('/',$path);

   array_shift($url_array);

   while ($url_array) {
      $_GET[$url_array[0]] = $url_array[1];
      array_shift($url_array);
      array_shift($url_array);
   }
}

Any ideas?

/ Tobias

+3
source share
4 answers

If you are using Apache, you need to take a look mod_rewrite.

Using mod_rewrite, you can change how URLs map to actual application endpoints. For your example, you need something like this:

RewriteEngine on 
RewriteRule ^/?page/(.*)$ page.php/$1 [L]

There are other simple examples on this page .

+5
source

, -, php .php. , apache. -

AddType application/x-httpd-php .php .php5 .php4 .php3 .phtml .phpt

<FilesMatch \.php$>
  SetHandler application/x-httpd-php
</FilesMatch>

. apache, , .php $, /, application/x-httpd-php (php).
.php . , . , , . <Directory> .
(, , ) php-, .htaccess

ForceType application/x-httpd-php

apache php script. ForceType .

+3

Apache .htaccess , page.php.

page.php (,/page.php), .htaccess:

RewriteEngine On
RewriteBase /

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

, , page.php let say/a_folder/another_folder/page.php

.htaccess:

RewriteEngine On
RewriteBase /

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

.

, page.php URL. , :

Instead of http://mydomain.com/page.php/foo/123/bar/456 you can use: htp: //mydomain.com/foo/123/bar/456

Good luck.

+2
source

Thank you for the answers, but if I use mod_rewrite, then I will make new rules (and many) every time I create a new page. And there are a lot of them :)

Maybe if I redirect all urls to my index.php with .htaccess and then fix my url like wordpress:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
0
source

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


All Articles