简体   繁体   中英

How do I customize my website post URL in PHP

All posts are in the posts table. I display each post on the web page the posts are identified by random numbers from the database (ex: 10110501 means post number 1 and so on.). Now, when a visitor click a post excerpt I want the post to display with the URL like this: www.example.com/posts/10110501 this will display post number 1 from the database to the single post page. I do not want the URL to have ? symbol. How can I achieve this?

Under posts directory you can add an index.php and also an '.htaccess' to rewrite any url to index.php

after successfull rewrtie rule all your posts request will hit that index.php and on that index.php you can get the url with $_SERVER['REQUEST_URI']

and then split the array take the random number and search it on database..

Sample '.htaccess' with rewrite rule

You need to use .htaccess reWrite Rules.

Suppose you want to have www.yourdomain.com/profile?id=user-unique-name as www.yourdomain.com/profile/user-unique-name Then in your .htaccess files you can write the following rules

RewriteEngine On
 
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
 
RewriteRule ^users/(\d+)*$ ./profile.php?id=$1

For references please read here more

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM