Cerco di spiegare questo problema passo dopo passo nel seguente esempio.
0) Domanda
cerco di chiedere a voi in questo modo:
voglio aprire la pagina come profilo facebook www.facebook.com/kaila.piyush
è ottenere l'ID da url e lo analizza al file profile.php e restituisce i dati featch dal database e mostra all'utente al suo profilo
normalmente quando sviluppiamo un sito web il suo collegamento appare come www.websit e.com/profile.php?id=username example.com/weblog/index.php?y=2000 & m = 11 & d = 23 & id = 5678
ora che aggiorniamo con il nuovo stile non riscrivere siamo utilizzare www.website.com/username o example.com/weblog/2000/11/23/5678 come permalink
http://example.com/profile/userid (get a profile by the ID)
http://example.com/profile/username (get a profile by the username)
http://example.com/myprofile (get the profile of the currently logged-in user)
1) .htaccess
Creare un file .htaccess nella cartella principale o aggiornare quello esistente:
Options +FollowSymLinks
# Turn on the RewriteEngine
RewriteEngine On
# Rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php
Che cosa fa?
Se la richiesta è per una directory o un file reale (uno esistente sul server), index.php non viene servito, altrimenti ogni URL viene reindirizzato a index.php.
2) index.php
Ora, vogliamo sapere quale azione per innescare, quindi abbiamo bisogno di leggere l'URL:
In index.php:
// index.php
// This is necessary when index.php is not in the root folder, but in some subfolder...
// We compare $requestURL and $scriptName to remove the inappropriate values
$requestURI = explode(‘/’, $_SERVER[‘REQUEST_URI’]);
$scriptName = explode(‘/’,$_SERVER[‘SCRIPT_NAME’]);
for ($i= 0; $i < sizeof($scriptName); $i++)
{
if ($requestURI[$i] == $scriptName[$i])
{
unset($requestURI[$i]);
}
}
$command = array_values($requestURI);
With the url http://example.com/profile/19837, $command would contain :
$command = array(
[0] => 'profile',
[1] => 19837,
[2] => ,
)
Now, we have to dispatch the URLs. We add this in the index.php :
// index.php
require_once("profile.php"); // We need this file
switch($command[0])
{
case ‘profile’ :
// We run the profile function from the profile.php file.
profile($command([1]);
break;
case ‘myprofile’ :
// We run the myProfile function from the profile.php file.
myProfile();
break;
default:
// Wrong page ! You could also redirect to your custom 404 page.
echo "404 Error : wrong page.";
break;
}
2) profile.php
Ora nel file profile.php, dovremmo avere qualcosa come t his:
// profile.php
function profile($chars)
{
// We check if $chars is an Integer (ie. an ID) or a String (ie. a potential username)
if (is_int($chars)) {
$id = $chars;
// Do the SQL to get the $user from his ID
// ........
} else {
$username = mysqli_real_escape_string($char);
// Do the SQL to get the $user from his username
// ...........
}
// Render your view with the $user variable
// .........
}
function myProfile()
{
// Get the currently logged-in user ID from the session :
$id = ....
// Run the above function :
profile($id);
}
Una grande risorsa mod_rewrite: http://www.addedbytes.com/apache/mod_rewrite-cheat-sheet/ –
Suggerisco il suo tutorial prima del cheatsheet: http://www.addedbytes.com/apache/url-rewriting-for-beginners/ –
Un'altra opzione, se si desidera eseguire la riscrittura dell'URL interamente in PHP, consiste nell'impostare regole di riscrittura per indirizzare tutte le richieste a un singolo script . Un grande vantaggio di questo è che puoi memorizzare il tuo codice in un'area non accessibile ad Apache, e solo il tuo script di dispatch URL deve essere accessibile da Apache. ad esempio: RewriteCond% {REQUEST_FILENAME}! -f RewriteCond% {REQUEST_FILENAME}! -d RewriteRule. dispatch.php Questo è simile al tuo esempio di forcetype. –