Help with PHP and htacess Friendly URL

Asked

Viewed 167 times

0

I am new to PHP programming, I created a dynamic website 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, with the PHP "switches" replacing the central content.

To request the exchange of content use the following link:

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

My URL looks like this: http://www.meusite.com.br/? pag=cotato. But I wish she’d stay that way: http://www.meusite.com.br/contato.

However, I’ve tried everything I know about PHP to find a solution, but I can’t fix it, please help me.

  • 1

    Welcome Marcos, edit your question and put the code, not the image, facilitates the staff help you

  • Thanks for the tip Pedro, I already made the change

4 answers

1

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

And work on it to suit you.

Make a explode by / check which will be the Dice in which the domain ends and the next vc know it will be the controller and where will direct.

  • $_SERVER['REQUEST_URI'] Already captures after / only do the explode and get the information if you have /contact us/phone /contact us/email

  • Hello Felipe, I am very beginner in PHP, I know very little even, I started now to study PHP. Much of what you explained did not understand anything! rsrsrs I’m sorry.

  • :) &#That we’re here to help. <?php $arrUri = explode("/", $_SERVER['REQUEST_URI']); switch ($arrUri[1]) { case 'home': include 'home.php'; break; case 'contact': include 'contact.php'; break; default : include 'home.php'; break; } ? > and try to access with the friendly url ("http://www.meusite.com.br/contact.")

0

I managed to solve!

In my file .htaccess I wrote the following:

RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?pag=$1

Daí criei um pequeno PHP pra receber o valor da variável 'pag':

$getUrl = strip_tags(trim(filter_input(INPUT_GET,'pag',FILTER_DEFAULT))); // Filtra o que é digitado na url
$setUrl = (empty($getUrl) ? 'home' : $getUrl);  //Define o valor padrão para a variável
$Url = explode('/', $setUrl); // Cria um array com o que vai sendo inserido na URL

0

I’m adding a new answer with 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 tries to access with the friendly url ("meusite.com.br/contact.")

-2

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

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

When the user tries to access for example http://meusite.com.br/faleconosco/, Apache will take anything that is placed between the two bars /(.*)/, in the 'faleconosco' case, and rewrite to the current format: http://meusite.com.br/? pag=faleconosco

Since you must have several other files on your site, the above code can make other Urls confusing, 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 to your . htaccess as follows:

RewriteEngine on
RewriteRule ^/home/?$ /?pag=home
RewriteRule ^/contato/?$ /?pag=contato
RewriteRule ^/faleconosco/?$ /?pag=faleconosco
  • I’m starting to mess with PHP a little while ago. It worked in part. I forgot to mention that before PHP and soon after it has a fixed HEADER and FOOTER and this PHP code only replaces the central content

  • So Antoni, I edited my question, I believe that now gives to better intender

  • What worked? What didn’t work? Mark, your code is correct. In fact, the links from the menu you made, can also be in the desired format, ie: only /home and /contact. To have the friendly Urls, you need to create . htaccess and teach apache how to interpret your site’s Urls. In my reply, I explained exactly how it would look for your case: take everything after the site and send to index.php as the complement of the pag variable.

  • The URL worked, but the HEADER and FOOTER were gone, they were meant to stay fixed and only change the page body.

  • if you want to view a site I’m doing: http://www.adrnet.com.br

  • Marcos, it didn’t work because HEADER and FOOTER tb are files on your site. The code I made, causes any URL to be rewritten to this format you requested. You can create rules only for the pages you do. Example: Rewriterule /contact$ /?pag=contact

Show 1 more comment

Browser other questions tagged

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