Generating friendly URL

Asked

Viewed 306 times

-1

Guys, I made a product registration page, and I’m willing to put to access the product through the name, same as the wordpress system, but I could not find any support for this.

When saving the product, it generates a friendly URL such as "www.example.com/product/product name" to be able to access it without having to use the ID in the link.

I already tried to work with the str_replace but causes problems with special characters such as spaces, quotes and others.

  • Have you tried fetch on the site before asking? Nothing cleared your doubts? So I believe your question needs improvement, because the way it is is unviable to answer anything.

  • I updated the expensive question.

1 answer

0

You can use a . htaccess file at the root of your website as follows:

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

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 at stackoverflow there are several other posts with this subject, a researched there.

  • So, man, I already use this part, but it’s the registration of the name as a URL that’s my problem. An example I have is wordpress, which uses the name of the post to access the post. I will update the question explaining better.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.