如何创建动态网址? [英] How do I create dynamic URLs?

查看:85
本文介绍了如何创建动态网址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个社交网络,允许用户撰写博客和提出问题。我想要创建动态网址,通过PHP在网址末尾发布博客或问题的标题。

I have a social network that allows users to write blogs and ask questions. I am wanting to create dynamic URLs that post the title of the blog or question on the end of the URL via PHP.

示例:

www.blah.com/the_title_here



<

Looking for the cleanest most efficient way to accomplish this.

推荐答案

您通常会将网址友好的slug存储在

You would usually store the URL-friendly "slug" in the database row, and then have a PHP script that finds posts matching that slug.

例如,如果您有一个名为 index.php的脚本

For example, if you have a script called index.php that took a parameter called slug...

<?php
if (isset($_GET['slug'])) {
    $sql = "SELECT * FROM `your_table` WHERE slug = ? LIMIT 1";
    $smt = $pdo->prepare($sql);
    $smt->execute(array($_GET['slug']));
    $row = $smt->fetchObject();
    // do something with the matching record here...
}
else {
    // display home page
}

...然后,您可以使用.htaccess重新写入请求:

...You could then re-write requests using .htaccess:

RewriteEngine on
RewriteRule ^(.+)$ index.php?slug=$1

这篇关于如何创建动态网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆