Rewriting URL: "index.php? Id = 5" in "home.html"

I am developing a website using php and the template.inc class. The problem is that I want to create a mini cms that allows the administrator to create an "html" page with these mysql attributes:

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

In architecture, if he created page number "5", the URL

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

But this is not very aesthetically pleasing, as I am very bad at rewriting url, even if I read a lot of manuals, I want to type the name + "html" to access the page.

If we take an example

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

if the administrator created a page with the following values:

id   : 5
name : 'home'

I want the user to be able to enter

"www.mywebsite.com/home.html" 

and without redirecting as I want this last url to still display and become the official url.

, , www.mywebsite.com/index.php?id=5 www.mywebsite.com/5.html... , , -, "" "home" (5 = > "home" ). URL?

, .

+3
3

.htaccess Wordpress, PHP. - ...

<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>

$_SERVER['REQUEST_URI'], , . .*, , .*\.html.

+5

,

Renesis $_SERVER['REQUEST_URI'] 'home.html'.

- :

<?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>";
}

?>

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

URL- Apache. many . Apache rewriting docs.

, , .

index.php id name=. , db , .

+1

Source: https://habr.com/ru/post/1733428/


All Articles