I have a selection of pages which currently have a url which looks something like this:
www.mydomain.com/directory/?id=123
This displays a story from a mySQL table with the id of 123.
What I would like to know is whether it is possible to alter the appearance of the URL to look something like this:
www.mydomain.com/directory/story-title
I have the following page code...
<?php
header('Content-type: text/html; charset=utf-8');
include $_SERVER['DOCUMENT_ROOT'].'/con.php';
$id = $_GET['id'];
$query = "SELECT * FROM mytable WHERE id = $id";
$result = $mysqli->query($query);
if ($mysqli->error) {
printf("Errormessage: %s\n", $mysqli->error);
}
$row = $result->fetch_array(MYSQLI_BOTH);
echo $row['title'];
?>
And the following in my .htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^directory/(.*)/?$ /directory/index.php?id=$1
The original URL works fine, but not the new one.
You cannot hide a variable in the URL if it is going to change from page to page... If the 'story-title' is unique for each page (it is a primary key in the database), then you can simple do an .htaccess
rewrite and use the 'story-title' as the id.
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^directory/(.*)$ directory/page.php?story=$1
However, if the 'story-title' is not unique then you will have to implement a different sort of rewrite that uses the id but also displays the story title. Exactly how SO does it.
The following will rewrite this:
www.domain.com/directory/ID/STORY_TITLE/
to
www.domain.com/directory/page.php?id=ID
.htacess (not tested):
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^directory/(.*)/(.*)/?$ directory/page.php?id=$1&story=$2
store story title in database and pass it ( story=story-title
) in URL instead of id=123
then write following .htaccess code
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^directory/(.*)$ directory/index.php?story=$1
now you can access www.mydomain.com/directory/?id=123
from www.mydomain.com/directory/story-title
I do not precicely know about it, but here is something what I read someday.
A special string is used as a title and ID at the same time. It is also called "slug". Slugs must be unique in your database.
Structure:
Title | Text | Slug | ID
------------+----------+-----------------+--------
"Big title" |"HTML etc"| "story-title" | 5
So you just use that slug to find a needed article.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.