URL Yeniden Yazma: "index.php id = 5"

3 Cevap php

I'm developping a website using php and the "template.inc" class. The problem is that I want to create a mini-cms that allows the admin to create an "html" page with these mysql attributes:

Table Name : Page
-----------------
id   :auto-icremented) 
name :varchar

O sayfa numarası "5" yarattı eğer mimaride, url

"ww.mywebsite.com/index.php?id=5".

Ben sayfaya erişmek için adı + "html" yazmak istiyorsanız, ben birçok öğreticiler okumak bile yeniden url çok kötüyüm Ama, bu çok esthectic böyle değildir.

Biz örnek alırsak

"www.mywebsite.com/index.php?id=5"

yönetici aşağıdaki değerlerle bir sayfa oluşturdu eğer:

id   : 5
name : 'home'

i kullanıcı yazabilirsiniz bunu istiyorum

"www.mywebsite.com/home.html" 

ve hiçbir yönlendirmesi ile ben bu son url hala görünür ve resmi url olmak gerektiğini istediğiniz gibi.

Thanks for your answer, i know how to rewrite www.mywebsite.com/index.php?id=5 to www.mywebsite.com/5.html ... but the problem is that i want, first, to get the "name" vale before and in my example, the name value is "home" (5 =>'home'). How can i access to my database with the url rewriting engine?

Thank you very much, regards.

3 Cevap

Bir PHP dosyası için her şeyi yönlendirmek için yüklemek standart Wordpress. Htaccess dosyasını kullanın. Bu gibi bir şey ...

<IfModule mod_rewrite.c>
    RewriteEngine On
    # Base is the URL path of the home directory
    RewriteBase /
    RewriteRule ^$ /index.php [L]

    # Skip real files and directories
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule .* /index.php [L]
</IfModule>

Sonra $_SERVER['REQUEST_URI'] kullanıcı aradığı anlamaya kullanın. Eğer gibi, kural daha spesifik yapmak istiyorsanız .* değiştirin .*\.html.

Zakaria,

Renesis en yazma kuralı kullandıktan sonra, $_SERVER['REQUEST_URI'] 'home.html' eşit olacaktır.

Try something like:

<?php

// clean
$page = mysql_real_escape_string($_SERVER['REQUEST_URI']);

// make query
$query = sprintf("select Page.* from Page where name = '%s'", $page);

// find page ID
if($result = mysql_query($query)){

  $page = mysql_fetch_assoc($result);

  echo "<pre>", print_r($page, true), "</pre>";
}

?>

Possible output

Array (
  [id]    => 5
  [name]  => 'home.html'
)

Bunun için Yeniden Yazma Apache URL'yi kullanın. Tek başına bu bu sitede many many örnekler vardır. Ayrıca resmi deneyebilirsiniz Apache rewriting docs.

Eğer veritabanı adını teklik zorlar, ya da sorunlar olacaktır emin olmak gerekir.

Edit

Index.php bir isim = parametresi yerine id parametresi almak var. Eğer db her sayfa isteği bir tablo taraması yapmıyoruz böylece endeksli adı alanını olduğundan emin olmak gerekir.