Questions about htaccess

1

I have a friendly link that would be localhost/artists/nome-do-artista/ , however I want to create pages within it eg Biography. localhost/artists/nome-do-artista/biography , only that my htaccess does not allow, how to solve?

I'm using:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php [NC,QSA]

In artists/index.php is doing a query in the database with name of the artist thus getting the link: artists/nome-do-artista .

But when I create artists/nome-do-artista/biography it looks for the main index that would be: artists/nome-do-artista .

I hope you understand, if you can help me, I'm flattered.

    
asked by anonymous 18.02.2018 / 17:48

2 answers

3

As I do not know how your folder structure is and how you are identifying the artist name and etc. I'll leave a simple answer, but you can adapt.

Folder Structure:

.
├── artists
│   ├── albums.php
│   ├── biography.php
│   └── index.php
└── index.php

.htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule artists/(.*)(?:/(.*))? /artists/index.php?artist=$1&page=$2 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php [L]

In the first rule of rewrite we define - through regex -, all access in http://www.example.com/artists/... must be redirected to the artists/index.php folder with the defined parameters .

Explaining Regex:

artists/(.*)(?:/(.*))?
└───┬──┘└─┬─┘└──┬────┘
    │     │     └──────── Captura o nome da página. O '?' indica que ele é opcional.
    │     └────────────── Captura o nome do artista
    └──────────────────── Base

artists / index.php

<?php

$artist = $_GET["artist"] ?? false;
$page   = $_GET["page"]   ?? "albums";

if ( $artist ) {

    $model = new ArtistModel();

    if( $model->has($artist) ) {        
        switch ($page) {
            case "biography":
                $content = $model->getBiography();
                require_once "biography.php";
                break;
            case "albums":
            default:
                $content = $model->getAlbums();
                require_once "biography.php";
                break;
        }

    }
} else {
    echo "Not found";
}
  

Attention! This is just a basic example. You should not use in production, just for studies and to be optimized / adapted to your code.

    
18.02.2018 / 18:25
1

Another response (not as good as the previous one), would be this htaccess (comments in the code):

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# redirecionada todas as urls que terminarem com /biography para a 
# pagina biografia.php (foge um pouco do padrão front controller),
# mas imagino que seja o que você queria inicialmente. 
# O ([a-zA-Z0-9\-]+) captura qualquer caracter alphanumerico incluindo 
# o -, e armazena em $1, que posteriormente é passado para a query
# como nomeartista, que pode ser acessado pelo global 
# $_GET ($_GET['nomeartista']).
# Além disso é passado o argumento [L] indicando que se houver um match 
# esse é o ultimo rewrite.
RewriteRule ^artists/([a-zA-Z0-9\-]+)/biography biografia.php?nomeartista=$1 [NC,QSA,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php [NC,QSA,L]

I do not know if it's within your means to use some framework to automate route creation, so take a look at FastRoute .

To delve into htaccess writing take a look at the apache mod_rewrite guide.     

18.02.2018 / 18:40