.htaccess configuration to access several PHP files within the same folder

2

I have been beating since yesterday with the configuration of my .htaccess , my original URL is like this (2 is the number of the page):

www.example.com/categoria/produtos/2 

Categoria is a folder within my public_html and produtos stays within it.

So far so good, but within categoria , I have several other .php files.

Currently my .htaccess looks like this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l 
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^(.*)$ produto.php?page=$1

In short, for all pages within my categoria , they are being redirected to produto.php and the pages do not pass, always the result stays the same!

The code structure looks like this:

  $url = $_GET['page']; //Pegando página selecionada na URL
  $dados = explode('/', $url);
  $dir = $dados[0]; 
  $page = $dados[1]; 

  if(empty($_GET['page'])){
    $page=1;
  }
  if($page >= '1'){
    $page = $page;
  }
  else{
    $page= '1';
  }

And in pagination like this:

echo "<li><a href='/categoria/produto.php/".($page+1)."'>NEXT</a></li>";
    
asked by anonymous 06.08.2014 / 22:17

3 answers

1

.HTACCESS

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} \.php$
RewriteRule (.*) index.php [QSA,L]


ROTA

// www.meusite.com/categoria/produtos/2 -> carros/novos
$url = ltrim( parse_url( $_SERVER['REQUEST_URI'] , PHP_URL_PATH ) , '/' );

$router= explode( '/' , $url );
$router[0] // categoria
$router[1] // produtos
$router[2] // 2

All your URL information will be in the router array.
You decide how to check the indexes of $router[X] .

There are several ways to route the URL, this is the simplest, but maintains the logic in PHP , making it easier to add, change or remove any category. / p>

Your HTACCESS will accept any URL, but it is up to PHP to validate and decide the controller responsible for each segment of the street route ...

  

www.domain.com/contact
  www.domain.com/category
  www.domain.com/category/search
  www.domain.com/category/products
  www.domain.com/category/products/2
  ... N combinations

    
07.08.2014 / 00:59
0

for url

http://www.meusite.com/categoria/produtos/556

Try this

RewriteEngine On
RewriteRule ^(categoria/produtos/[0-9]+)$ produto.php?page=$1

or

RewriteEngine On
RewriteRule ^([a-z]+/[a-z]+/[0-9]+)$ produto.php?page=$1

dai no php producto.php

<?php  
$url = $_GET['page']; //Pegando página selecionada na URL  
$dados = explode('/', $url);  
$dir = $dados[0];  
$subdir = $dados[1];  
$page = $dados[2];  

if(empty($_GET['page'])) {  
$page = 1;

} else if ($page >= '1') {  
$page = $page;  

} else {  
$page= '1';  

}

This site is good for simulating rewrite .htaccess link

    
06.08.2014 / 22:51
0

Before answering the question itself, an important detail that can cause a lot of confusion in your tests: do not forget to set the base path!

This can be done in .htaccess, but the simplest way is to do the same html:

<!-- inclua isso no "head" do html -->
<base href="http://a_url_do_seu_site.com/" />

So, especially when working with many folders and subfolders, files with sections and subsections, do not miss the absolute beginning of navigation for reference of all links.

In your case , if "categories" is always the basis, you could do this:

<!-- inclua isso no "head" do html -->
<base href="http://www.meusite.com/categoria/" />

And all the return of friendly urls would start with "product / page", ignoring the categories, which would be your "home", so to speak.

Returning to your problem, this kind of situation is what we call "multiple entries" when there is more than one .php file to call, instead of just one index.php. In this case, .htaccess can be configured in two ways.

First Choice

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?p=$1

Here it does not change anything compared to the single-entry case, but the existing files are also called by the "friendly" query string. To do this, simply agree that the first parameter (in your case) is a folder, and the second is the name of the file. For example:

www.meusite.com/categoria/produtos/2 

extracting the query srtring:

$qs = explode("/", ltrim($_GET['p'], "/"));

$caminho = $qs[0];
$arquivo = $qs[1].".php";
$parametro = $qs[2];
// e assim sucessivamente...

The most important thing about this method is that you need to work out a way to standardize the addresses and file and folder structure of your project. It's easier (and beneficial in many ways) to plan the project well, than to get match-burning with .htaccess.

Second Option (not recommended, but exists)

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1\.php

What changes, is that you can now pass full paths, and the last parameter (without slash) will be the name of the php file. I do not even need to say that there are no advantages to this approach in your case, mainly because it makes it difficult to pass query strings.

Finally, I see something that could be changed in your paging code, which looks like this:

if(empty($_GET['page'])){ 
    $page=1;              
}                         
if($page >= '1'){
    $page = $page;
} else {
    $page= '1';
}

When would it be more interesting to do this:

$page = 1;                   // por padrão, page é sempre 1
$dir  = "produtos";          // outro padrão, por exemplo
if (!empty($_GET['page'])) { // todo o resto só faz sentido se houver dados
    $dados = explode("/", ltrim($_GET['pages'], "/"));   
    $dir  = $dados[0];
    $page = empty($dados[1]) ? 1 : $dados[1]; // só por garantia...
}
    
07.08.2014 / 02:19