让 URL 动态看起来不错

发布于 2024-11-02 10:43:45 字数 1073 浏览 2 评论 0原文

可能的重复:
如何转换 PHP查询字符串转换为基于斜杠的 URL?

如果我有数据库驱动的页面,其 url 如下所示:

http://www.companyname.com/dynamic_page.php?id=1
http://www.companyname.com/dynamic_page.php?id=2
http://www.companyname.com/dynamic_page.php?id=3
...
http://www.companyname.com/dynamic_page.php?id=4001
http://www.companyname.com/dynamic_page.php?id=4002
http://www.companyname.com/dynamic_page.php?id=4003

url 中的 id 用于从数据库获取正确的内容,并且用户可以添加/删除/编辑任意数量的页面。

我了解如何手动使用 mod_rewrite 使网址看起来更好,例如

http://www.companyname.com/individual/1
http://www.companyname.com/individual/2
http://www.companyname.com/individual/3
...
http://www.companyname.com/commercial/4001
http://www.companyname.com/commercial/4002
http://www.companyname.com/commercial/4003

但是我将如何动态地做到这一点?即允许用户指定 url 的一部分,在这种情况下,用户选择了“个人”或“商业”,然后在 url 末尾添加 id,而无需我每次都编辑 .htaccess 文件用户创建新页面,或编辑/删除页面。

Possible Duplicate:
How do I convert a PHP query string into a slash-based URL?

If I have database driven pages with urls like this:

http://www.companyname.com/dynamic_page.php?id=1
http://www.companyname.com/dynamic_page.php?id=2
http://www.companyname.com/dynamic_page.php?id=3
...
http://www.companyname.com/dynamic_page.php?id=4001
http://www.companyname.com/dynamic_page.php?id=4002
http://www.companyname.com/dynamic_page.php?id=4003

Where the id in the url is used to get the correct content from the database, and the user has the ability to add/remove/edit as many pages as s/he wants.

I understand how to manually use mod_rewrite to give the urls a nicer look e.g.

http://www.companyname.com/individual/1
http://www.companyname.com/individual/2
http://www.companyname.com/individual/3
...
http://www.companyname.com/commercial/4001
http://www.companyname.com/commercial/4002
http://www.companyname.com/commercial/4003

But how would I do that dynamically? i.e. allow the user to specify part of the url, in this case the user has selected either "individual" or "commercial", then add the id at the end of the url, without me having to edit the .htaccess file each and everytime a user makes a new page, or edits/deletes a page.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

倚栏听风 2024-11-09 10:43:45

我认为这已经足够了:

RewriteRule http://www.companyname.com/(.*)/(\d+) http://www.companyname.com/dynamic_page.php?id=$2

/4441之前是什么并不重要,它总是会导致dynamic_page.php?id=4441...

I think that this could be enough:

RewriteRule http://www.companyname.com/(.*)/(\d+) http://www.companyname.com/dynamic_page.php?id=$2

It doesn't matter what is before /4441, it will always lead to dynamic_page.php?id=4441...

つ低調成傷 2024-11-09 10:43:45

您是否手动为 ht access 文件中的每个 ID 添加新行?

您可以使用正则表达式轻松完成所有这些。这是一篇有点旧的帖子,但完全符合您的需求:http://www.webmasterworld。 com/forum92/4332.htm

Are you manually adding a new line for every ID in the ht access file?

You could accomplish all of this easily with a regular expression. Here's a post that's a bit old but is completely relative to your needs: http://www.webmasterworld.com/forum92/4332.htm

走过海棠暮 2024-11-09 10:43:45

试试这个:

RewriteRule http://www.companyname.com/(.*)/(\d+) http://www.companyname.com/index.php?firstvalue=$1&secondvalue=$2

Try this:

RewriteRule http://www.companyname.com/(.*)/(\d+) http://www.companyname.com/index.php?firstvalue=$1&secondvalue=$2
三五鸿雁 2024-11-09 10:43:45

查看-> 清理网址

你可以尝试一下。

RewriteEngine On
RewriteRule ^companyname.com/(.*)/(\d+)$ /$1.php?id=$2

Check out -> Clean Urls

You can try.

RewriteEngine On
RewriteRule ^companyname.com/(.*)/(\d+)$ /$1.php?id=$2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文