I do not know the logic will be the same, but because there is no example of the part containing the html of your example, I created a very simple one.
<!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 containing PHP remained unchanged, except in some parts, yet most of it prevails. The .htaccess
file is in the root
directory of the current instance, along with index.php
, which contains the above code. The links were written by me, and are not generated dynamically from a database, or any other source, and I put target="_blank"
in them to facilitate at the time of testing. My home.php file is equivalent to your default file.
In the same directory, I created two more folders, respectively error and temp .
Root
- .htaccess
- index.php
- error /
- temp /
In the error folder, I saved a file with the name 404.html containing the error message that appears every time the page is not found.
In the temp folder I saved the files home.php and contact.php containing the code <?php echo "Página principal"; ?>
and <?php echo "Página de contacto"; ?>
/ p>
The .htaccess file I used is just the same as 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
In this way I can write the links in 3 different ways.
- home
- home.html
- home-10.html (the type of link I used in this example).
#Edit
I could get a doubt where this code is located in the index.php it is also the main part of my code and I noticed that you used home that your home is equivalent to my index.php in case I would change my default to index or your home to index to fit it to my site?
In this case, where you have if(file_exists(...)){ #instrução# }
, you would have to put the code below, instead of #instrução#
. However, if you worked with includes, it could make you easier on many things, and you organized the content better.
#
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 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 will solve your current problem, I do not recommend this practice. There are many, many ways to do this with enjoyable results. I encourage you to learn more about friendly URLs .