I do not know the logic will be the same, but because there is no example of the part that contains the html from your example, I created a very simple.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<p><a href="home-10.html" target="_blank">Home</a> | <a href="contacto-12.html" target="_blank">Contacto</a> | <a href="ajuda-14.html" target="_blank">Ajuda</a></p>
<?php
$p = isset($_GET['pag']) ? $_GET['pag'] : 'home';
if(isset($p)){
$pasta = 'temp';
if(file_exists($pasta . '/' . $p .'.php')){
include_once $pasta . '/' .$p . '.php';
} else {
header('location: error/404.html');
exit();
}
}
?>
</body>
</html>
As you can see, the part that contains the PHP remained unchanged, except in some parts, yet most of it prevails. The file .htaccess
is in the directory root
of the example in use, together with the index.php
, containing the above code. The links were written by me, and are not generated dynamically from a database, or any other source, and I put in them a target="_blank"
for ease when testing. My file home php. is equivalent to your file default.
In the same directory, I created two more folders, respectively error and temp.
Root
- .htaccess
- index php.
- error/
- temp/
In the folder error saved a file with the name 404.html containing the error message displayed whenever the page is not found.
In the folder temp saved the files home php. and php contact. containing respectively the code <?php echo "Página principal"; ?>
and <?php echo "Página de contacto"; ?>
.
The file .htaccess I used, it’s just like yours, except I just changed the expression to something more specific.
RewriteEngine On
RewriteRule %{REQUEST_FILENAME} !-f
RewriteRule %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z]+)$ index.php?pag=$1
RewriteRule ^([A-Za-z]+).html$ index.php?pag=$1
RewriteRule ^([A-Za-z]+)-([0-9]+).html$ index.php?pag=$1
This way I can write the links in 3 different ways.
- home
- home html.
- home-10.html (the type of link that I used in this example).
#Edit
could make me doubt where this my code is located in index.php it also and the main part of my code and I noticed that you used home that your home equivalent to my index.php in case I would change my default to index or your home to index to adapt it to my site?
In this case, where do you have if(file_exists(...)){ #instrução# }
, you’d have to put the code down instead of #instrução#
. However, if you worked with includes, I could make it easier for you in a lot of ways, and you’d better organize the content.
#
if($p === 'home'){
/*
* Retorno ou mensagem a ser exibida na página pricipal, quando nenhuma outra está selecionada
*/
echo "Página principal";
} else {
include_once $pasta . '/' .$p . '.php';
}
#
#Edit 2
Replace all the php code in your index.php
and put this in place of the old code:
$p = isset($_GET['url']) ? $_GET['url'] : 'default';
if(isset($p)){
$pasta = 'temp';
if(file_exists($pasta . '/' . $p .'.php')){
#
if($p === 'default'){
/*
* Retorno ou mensagem a ser exibida na página pricipal, quando nenhuma outra está selecionada
*/
echo "Página principal";
} else {
include_once $pasta . '/' .$p . '.php';
}
#
} else {
if($p === 'default'){
echo "Página principal";
} else {
header('location: error/404.html');
exit();
}
}
}
Although this solves your current problem, I do not recommend this practice. There are many and many ways to do this with pleasant results. I recommend you find out more about Friendly Urls.
Hello Leonardo, have you checked the apache configuration file if it supports Override? If it does not support, these settings have to be in the site’s configuration file (i.e. htaccess will not work)
– Rafael Mena Barreto
yes I checked!
– Leonardo Costa
Leonardo if the Nav folder is above Document root, don’t you have to touch that file_exists there? It would have to be '.. /'. $folder/pag.php
– Rafael Mena Barreto