将基于参数的 URL 转换为漂亮的 URL
目前我的网址格式如下:
http://www.domain.com/members/username/
这很好。
然而,每个用户可能有几首与其帐户相关联的“歌曲”。
单首歌曲的 URL 如下所示:
http://www.domain。 com/members/username/song/?songid=2
最后的数字显然指的是MySQL数据库中的ID。
使用 jQuery/javascript,从 URL 收集 ID,然后查询数据库并呈现相关歌曲/页面。
我想将这些 URL 更改为以下格式:
http://www.domain .com/members/username/song/songname/
但我完全不知道该怎么做。我已经阅读了大量有关该主题的文章,但没有发现任何与我的情况非常相关的内容。
更复杂的是,歌曲名称并不总是唯一的。例如,如果我们想象歌曲名称“hello”,则数据库中很可能存在另一首具有相同名称的歌曲,尽管歌曲 ID 不同。
鉴于您在这个问题中收到的信息有限,我对更笼统的答案非常满意,描述了要采取的方法。
一般信息:
- Apache/Nginx 代理
- 后端:PHP
- jQuery/Javascript 前端
Currently I have url's in this format:
http://www.domain.com/members/username/
This is fine.
However each user may have several 'songs' associated with their account.
The url's for the individual song's look like this:
http://www.domain.com/members/username/song/?songid=2
With the number at the end obviously referring to the ID in the MySQL database.
Using jQuery/javascript, the ID is collected from the URL and the database is then queried and the relevent song/page is rendered.
I would like to change these URL's to the following format instead:
http://www.domain.com/members/username/song/songname/
But I have absolutely no idea how to go about it. I've been doing quite a bit of reading on the subject but haven't found anything quite relevant to my situation.
To further compound the challenge, song names are not always unique. For instance if we image the song name 'hello' it is quite possible that another song may exist in the database with the same name, albeit with a different song ID.
Given the limit information you are recieving in this question I am quite content with more generalised answers, describing the approach to take.
General info:
- Apache/Nginx proxy
- Backend: PHP
- jQuery/Javascript front end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道如何在数据库中存储歌曲,但有一个想法:
使用URL重写将members/username/song/songname/重写为song.php?user=username&song=songname。这里有很多教程,或者尝试使用 URL 重写生成器工具 。
在song.php 中,获取这些GET 值。执行 MySQL 查询,其中歌曲名和用户名匹配。输出结果。
注意:用户必须只能存储一首具有给定名称的歌曲。此外,必须存储存储用户的姓名。否则这是不可能的。
I don't know how do you store songs in the database but an idea:
use URL rewrite to rewrite members/username/song/songname/ to song.php?user=username&song=songname. There are plenty of tutorials here or perhaps try to use an URL rewrite-generator tool.
In song.php, get these GET values. Do a MySQL query where the songname and the username match. Output the result.
Note: it is OBLIGATORY to make that a user can store only one song with a given name. Also, the storing user's name MUST be stored. Else this is impossible.
简单的 Apache 重写,在主 httpd.conf 文件中,或者如果您无权访问主配置文件,则 htaccess 文件就足够了
Simple Apache rewrites, in the main httpd.conf file, or an htaccess file if you don't have access to the main config file should suffice