How to manually create friendly URLs? (Php)

How to manually create friendly URLs? (PHP) So I created a simple php file that requested an echo request. Now it has the form echo.php?string=bla+bla+bla&font=times I want to see it as echo/bla+bla+bla/timesHow to do it (without using external libraries)?

+3
source share
6 answers

Similar to the idea of ​​a 404 handler, with a very simple RewriteRule, you can easily move the URL rewrite control to PHP, which might be easier for you:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . rewrite.php [L]

rewrite.php URL- RegEx. URL- ( $_SERVER['REQUEST_URI']) URL- . $_GET .

:

$rules = array(
    array(
        'articles/view/([0-9]+)',
        'articles.php?id=$1'
    ),
    array(
        'articles/delete/([0-9]+)',
        'articles.php?id=$1&action=delete'
    ),
);
+2

mod_rewrite Apache. .htaccess - :

RewriteEngine On
RewriteRule ^echo/(.+?)/(.+)$ echo.php?string=$1&font=$2 [L]

mod_rewrite. , , PHP, .

+5
+3

echo.php echo, -. Apache, mod_rewrite .

$_SERVER['REQUEST_URI'] regex. mod_rewrite , , , PHP.

+2

.htaccess, Zend Framework. URL.

+1

URL- mod_rewrite, php 404. URL- , .

 $path_parts = explode('/', $_SERVER['PATH_INFO']);
 echo $path_parts[0]. ', '.$path_parts[1].' font';

For performance reasons, you don’t need to configure apache so as not to send certain types of files (e.g. gif, jpg, png, etc.) to PHP for processing.

Despite the fact that you can do several different actions, as indicated in the answers, you cannot do this without configuring the configurations on the web server.

0
source

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


All Articles