Help with Friendly URL with PHP and htacess

0

I'm new to programming in PHP, I created a dynamic site using the following code:

   <nav>
        <ul class="navbar-nav">
            <li class="nav-item">
                <a class="nav-link" href="?pag=home">Home</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="?pag=contato">Contato</a>
            </li>
        </ul>
    </nav>

    <?php
    $paginas = @$_GET['pag'];
    switch ($paginas) {
        default :
            include 'home.php';
            break;

        case 'home':
            include 'home.php';
            break;

        case 'contato':
            include 'contato.php';
            break;
    }
    ?>

    <footer>

    </footer>

The navigation bar and footer are fixed, having the PHP "switch" exchanging the central content.

To request the exchange of content I use the following link:

        <li class="nav-item">
            <a class="nav-link" href="?pag=contato">Contato</a>
        </li>

My URL looks like this: link . But I'd like it to look like this: link .

However, I have tried everything I know about PHP to find a solution, but I can not solve it, please help me.

    
asked by anonymous 24.08.2018 / 22:08

3 answers

0

You can get the URI $ _SERVER ['REQUEST_URI'];

And work on it to meet you.

Make an exploit by / check what will be the index in which the domain ends and the next you know that will be the controller and where it will direct.

    
24.08.2018 / 22:38
0

I'm adding a new response with the code to be tested

<?php
        $arrUri = explode("/", $_SERVER['REQUEST_URI']);
        switch ($arrUri[1]) {
            case 'home':
                include 'home.php';
                break;
            case 'contato':
                include 'contato.php';
                break;
            default :
                include 'home.php';
            break;
        }
?>

and try to access with the friendly url ("myite.com / contact.")

    
27.08.2018 / 01:10
-1

Assuming you are using Apache2 with mod_rewrite enabled on your PHP server, you should create a .htaccess file at the root of your site with the following code:

RewriteEngine on
RewriteRule ^/(.*)/$ /?pag=$1

When the user tries to access for example link , Apache will pick up anything that is placed between the two bars / ( . *) /, in the 'faleconosco' case, and rewrite to the current format: link

Because you must have several other files on your site, the above code may leave other confusing URLs, such as the header and footer of your site that you mentioned in the comments. To avoid this, you can redirect each of the pages specifically in your .htaccess as follows:

RewriteEngine on
RewriteRule ^/home/?$ /?pag=home
RewriteRule ^/contato/?$ /?pag=contato
RewriteRule ^/faleconosco/?$ /?pag=faleconosco
    
24.08.2018 / 23:43