apache mod_rewrite:使用数据库更新重写规则

发布于 2024-10-04 11:36:08 字数 399 浏览 0 评论 0原文

mod_rewrite 完全是新手。

假设我想为我网站上的每个制造商创建漂亮的 URL, 所以我有 www.mysite.com/samsung www.mysite.com/sony www.mysite.com/acme

运行良好。

但是,如果我有数百个制造商并且它们不断变化,那么怎么办?在某处有一些关于重写映射的模糊参考,但没有任何解释它,也没有教程。有人可以帮忙吗?

另外,为什么这个问题不是 mod_rewrite 教程中涵盖的主要主题?当您必须手动维护 mod_rewrite 时(假设您的网站上偶尔有新内容),mod_rewrite 有什么用处?

还提到需要访问 httpd.conf 如何访问托管提供商服务器上的 httpd.conf?其他网站是如何做到这一点的?

谢谢

Total newbie at mod_rewrite.

Let's say I want to create nice URLs for every manufacturer on my site,
so I have
www.mysite.com/samsung
www.mysite.com/sony
www.mysite.com/acme

works well enough.

However, if I have hundreds of manufacturers and if they're changing constantly, what then? There are some vague references for something called rewrite map somewhere but nothing that explains it and no tutorials. Can anyone help?

Also, why is this problem not the main topic covered in tutorials for mod_rewrite? How is mod_rewrite possibly useful when you have to maintain it manually (assuming you have new content on your site once in a while)?

There is also mention of needing to have access to httpd.conf
How do I access httpd.conf on my hosting provider's server? How does every other site do this?

Thanks

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

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

发布评论

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

评论(2

情愿 2024-10-11 11:36:08

在搜索类似的解决方案时刚刚遇到这个答案 - 进一步搜索我发现 mod_rewrite 现在具有 RewriteMap 指令,它将完全执行您想要的操作,而无需运行 PHP 或其他脚本语言。

它允许您使用文本文件、DBM 文件、外部脚本或 SQL 查询定义映射规则。

我希望这有帮助!

Just came across this answer while searching for a similar solution — searching a bit further I discovered that mod_rewrite now has the RewriteMap directive, which will do exactly what you want without the need to run PHP or another scripting language.

It lets you define a mapping rule with a text file, a DBM file, an external script or an SQL query.

I hope that helps!

别靠近我心 2024-10-11 11:36:08

通常完成此操作的方法是,您将获取与特定模式匹配的所有 URL,并将它们路由到 PHP 文件(或任何服务器端编程语言),以实现更复杂的路由。像这样的事情:

RewriteRule ^(.*)$ myroute.php?url=$1 [QSA,L]

然后,在 myroute.php 文件中,您可以包含查看“url”查询字符串参数的逻辑,因为它将包含传入的原始 URL。也许您可以匹配将其发送给数据库中的制造商,或者任何其他需要的信息。

此示例显然采用所有 URL 并将它们映射到 myroute.php。另一个例子可能是这样的:

RewriteRule ^/manufacturers/(.*)$ manuf.php?name=$1 [QSA,L]

在这种情况下,它将像这样映射 URL:

/制造商/索尼=> /manuf.php?name=索尼
/制造商/三星=> /manuf.php?name=三星
等等...

在这种情况下,您的 manuf.php 文件可以根据 name 查询字符串参数查找数据库。

The way this would typically be done is that you would take all URLs that match a specific pattern and route them to a PHP file (or whatever your server-side programming language is) for more complex routing. Something like this:

RewriteRule ^(.*)$ myroute.php?url=$1 [QSA,L]

Then, in your myroute.php file, you can include logic to look at the "url" query string parameter, since it will contain the original URL that came in. Perhaps you could match it to a manufacturer in the database, or whatever else is required.

This example obviously takes all URLs and maps them to myroute.php. Another example might be something like:

RewriteRule ^/manufacturers/(.*)$ manuf.php?name=$1 [QSA,L]

In this case, it will map URLs like so:

/manufacturers/sony => /manuf.php?name=sony
/manufacturers/samsung => /manuf.php?name=samsung
etc...

In this case, your manuf.php file could look up the database based on the name query string parameter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文