Generating friendly URL

-1

Well, I've made a product registration page, and I'm trying to get access to the product through the name, just like the wordpress system, but I could not find any support for that.

When you save the product, it generates a friendly URL such as "www.example.com/product/productname" so you can access it without having to use the ID on the link.

I have already tried working with str_replace but it causes problems with special characters like spaces, quotation marks and others.

    
asked by anonymous 19.03.2018 / 01:18

1 answer

0

You can use a .htaccess file in the root of your site as follows:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?pag=$1 [L,QSA]
</IfModule>

In this way you will create a file called index.php, which calls a function such as the following function:

    function getPage() {
    if (isset($_GET['pag'])) {

        $pagina = $_GET['pag'];

        $pag = explode("/", $pagina);

        $pagina = $pag[0] . '.php';

        if (file_exists($pagina)) {
            include $pagina;
        } else {
            include 'error.php';
        }
    }
}

There are other ways, and here in stackoverflow there are several other posts with this subject, from the one researched there.

    
19.03.2018 / 01:27