Send and pick up GET by AMIGAVEL URL

Asked

Viewed 1,177 times

5

I have this tag on the index

<a href="projeto">Projeto</a>

Who sends me to this page

localhost/project.php

That I’m leaving like this

localhost/project

With the

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php
DirectoryIndex home.php home.html index.php index.html

But now I need to pass two variables through the url, it would look like this

<a href="projeto?id=25&nome=aqui_vai_o_nome">Projeto</a>

But I need the URL to look like this

localhost/project/25/aqui_vai_o_name

Remembering that later I need to get these two data inside the page PROJECT

This is the code of the.php project

<!DOCTYPE html>
<html lang="pt-br">

<head>
    <meta charset="UTF-8">
    <title>Document</title>

    <?php include('include/baseHead-2.php'); ?>
    <!-- Bootstrap CSS -->    
    <?php include('include/fonts.php'); ?>
    <?php include('include/css-2.php'); ?>
</head>

<body>
    <?php include('include/topbar-2.php'); ?>
    <div class="container-flex m-primary container-title-page">
        <div class="container py-80">
            <div><h1 class="ff-os">Titulo da pagina</h1></div>
            <div><h2 class="ff-os"><a href="../../home" class="ff-os">Home</a> / Pagina atual</h2></div>
        </div>
    </div>
    <div class="container py-100 container-*">
        <div class="row h-200px">
            <?php
                echo $_GET['id'];
            ?>
        </div>
    </div>
    <?php include('include/footer-2.php'); ?>
</body>

<?php include('include/js-2.php'); ?>

</html>

This is my . htaccess and the new.php project where you’re not getting $_GET

.htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^([0-9A-z_-]+)$ $1.php

RewriteRule ^projeto\/([0-9A-z_-]+)\/([0-9A-z_-]+)$ projeto.php?id=$1&nome=$2

DirectoryIndex home.php home.html index.php index.html

project.php - "project/15/name"

<?php

    echo $_GET['id']."</br>";
    echo $_GET['nome'];

?>
  • Unfortunately, I can’t see what’s wrong. If you want I can create another answer with another type of structure for you to test.

1 answer

1

You can do it like this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^([0-9A-z_-]+)$ $1.php

RewriteRule ^projeto\/([0-9A-z_-]+)\/([0-9A-z_-]+)$ projeto.php?id=$1&nome=$2

DirectoryIndex home.php home.html index.php index.html

This line below:

RewriteRule ^projeto\/([0-9A-z_-]+)\/([0-9A-z_-]+)$ projeto.php?id=$1&nome=$2

Create a different rule, just in case the URL starts with projeto.

To get the id and the name just use the $_GET['id'] and the $_GET['nome'] inside the archive projeto.php

If you click on:

<a href="projeto/15/nome_do_projeto">ver projeto</a>

In projeto.php you will be able to view using the echo:

   echo $_GET['id']."</br>";
   echo $_GET['nome'];

Return:

15
nome_do_projeto

EDIT: Problems accessing other files

When you use friendly url it is necessary to put the full path from where the image, css files and js are. In your case it would look like this, an example:

<img src="http://localhost/imagem/imagemExemplo.js"/>
<link rel="stylesheet" type = "text/css" href="http://localhost/res/style/estilo.css" media="screen" />

I usually create a constant and use it throughout the site:

define("URL", "http://localhost/");
<link rel='stylesheet' type = 'text/css' href='<?php echo URL; ?>/style/estilo.css' media='screen' />

This makes it easier, because when you publish the site, just change the url.

Edith 2:

Change the .htaccess for:

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f 
RewriteCond %{SCRIPT_FILENAME} !-d

RewriteRule ^([0-9A-z_-]+)$ $1.php

RewriteRule ^projeto\/([0-9A-z_-]+)\/([0-9A-z_-]+)$ projeto.php?id=$1&nome=$2

DirectoryIndex home.php home.html index.php index.html

Browser other questions tagged

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