htaccess url重写(数据库访问)?

发布于 2024-12-13 02:33:28 字数 458 浏览 0 评论 0原文

我有一个带有以下参数的网页。

http://www.somesite.com/community_details.php ?comm_id=233&region_id=2&city_id=40

我希望将其更改为这个

http://www.somesite.com/virginia/fairfax/some_community/

如何致电db 返回地区、城市、社区以在 .htaccess 中进行 url 重写?

I have a web page with the following parameters.

http://www.somesite.com/community_details.php?comm_id=233®ion_id=2&city_id=40

I would like it changed to this

http://www.somesite.com/virginia/fairfax/some_community/

How do I call db to return region, city, community for a url rewrite in .htaccess?

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

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

发布评论

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

评论(5

厌倦 2024-12-20 02:33:28

它不是特别有效,但是您可以使用 RewriteMap 来使用一个外部 txt/dbm/程序,让 mod_rewrite 根据结果进行查找和重写。

RewriteMap pretty-community prg:/path/to/some/shell/script

RewriteRule community_details.php?(.*) ${pretty-community:$1}

从community_details.php脚本捕获的查询字符串将被传递到其stdin上的指定外部脚本,并且该脚本通过其stdout回复重写的url。

请注意,该脚本在 Apache 首次启动时启动一次,然后基本上以守护进程模式运行,为每次执行的重写与 Apache 进行通信。由于 PHP 不太适合编写守护程序,因此您可能想用其他语言来编写此程序。

It's not particularly efficient, but you CAN use a RewriteMap to use an external txt/dbm/program to let mod_rewrite do lookups and rewrite based on the results.

RewriteMap pretty-community prg:/path/to/some/shell/script

RewriteRule community_details.php?(.*) ${pretty-community:$1}

The captured query string from the community_details.php script would be passed to the specified external script on its stdin, and the script replies with the rewritten url via its stdout.

Note that the script is started ONCE when Apache first fires up, and then essentially runs in daemon mode, communicating with Apache for every rewrite performed. Since PHP isn't particularly suitable for writing daemons, you might want to do this program in some other language.

我的黑色迷你裙 2024-12-20 02:33:28

我认为; 如果您

的链接是 http://www.somesite.com/virginia/fairfax/ some_community/

您可以使用 htacces 将其更改为 http://www.somesite.com/community_details.php?comm_code =samo_community®ion_code=virginia&city_code=fairfax

并采取数据库中要在 php 中使用的代码 id。

I think; you can't

If your link is http://www.somesite.com/virginia/fairfax/some_community/

you could change it with htacces as http://www.somesite.com/community_details.php?comm_code=samo_community®ion_code=virginia&city_code=fairfax

and take id of codes from database to use in php.

夏有森光若流苏 2024-12-20 02:33:28

阅读本文是一个好的开始:
http://httpd.apache.org/docs/current/mod/mod_rewrite .html#rewritemap

您为您的模式创建一个正则表达式,然后编写一个外部 Perl/PHP 脚本来实现对 MySQL 表的哈希查找并将其返回到 Apache。

Reading this is a good start:
http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritemap

You create a regex for your pattern, then write an external Perl/PHP whatever script that implements a hash lookup against a MySQL table and returns it to Apache.

国产ˉ祖宗 2024-12-20 02:33:28

嗯,这很简单。
只需将所有虚拟请求路由到 php scritp 即可查询数据库。

RewriteCond  %{REQUEST_FILENAME} !-f
RewriteCond  %{REQUEST_FILENAME} !-d
RewriteRule  ^(.*)$ index.php?request=$1 [QSA,L]

并且您将在 $_GET['request'] 中请求路径。

使用爆炸将其拆分并从数据库获取参数。

Well, it's quite easy.
just route ALL virtual requests to php scritp which will query a database.

RewriteCond  %{REQUEST_FILENAME} !-f
RewriteCond  %{REQUEST_FILENAME} !-d
RewriteRule  ^(.*)$ index.php?request=$1 [QSA,L]

and you will have requested path in $_GET['request'].

split it up using explode and get your parameters from db.

念三年u 2024-12-20 02:33:28

你不能。

您可以做的是让community_details.php 进行数据库查找,然后调用die(header("Location: blahblahblah"));。但是,您必须使用 .htaccess 和其他所有内容来处理该内容,因此根本不这样做可能会更容易 - 取决于此类 URL 对您来说有多重要。

You can't.

What you can do is have community_details.php do the database lookup, then call die(header("Location: blahblahblah"));. But then you would have to process that back using .htaccess and everything else, so it might be easier to just not do that at all - depends on how important such URLs are to you.

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