是否有可能使用mod_rewrite htaccess的重写一个URL,从名称到ID。有可能、重写、名称、mod_rewrite

2023-09-02 11:25:41 作者:就你瞎逼逼

如果我有一个网址 http://www.domain.com/listing.php?company_id=1 有可能是我重新写为 http://www.domain.com/company-name 通过使用该ID从数据库中获取的名称。

If I have a URLhttp://www.domain.com/listing.php?company_id=1 is it possible for me to re-write that to http://www.domain.com/company-name by using that id to pull the name from the database.

或者我必须改变listing.php,使其?COMPANY_NAME =公司名称

Or do I have to change listing.php to make it ?company_name=company-name

如果任何人都可以在正确的方向,将是巨大指向我。

If anyone can point me in the right direction that would be great.

谢谢!

推荐答案

一个地图功能允许使用在URL中没有id可言,而仍然使用的ID在重写URL做数据库查询。

A map function allows using no id in the url at all, while still using the id in the rewritten url to do the database lookup.

http://www.domain.com/another-one

映射到

http://www.domain.com/listing.php?company_id=3423

下面是我如何使用地图的文本文件,这是我从一个数据库查询生成。我相信了mod_rewrite也做映射到直接的数据库,其中有我不熟悉的(也许有人可以提供答案)。我用的Helicon技术的V3的ISAPI_Rewrite,它的工作原理是mod_rewrite的,所以这应该为你工作。

Here is how I use map text files, which I generate from a database query. I believe mod_rewrite also does mapping to a database directly, of which I'm unfamiliar (maybe someone can provide that answer). I use Helicon Tech's isapi_rewrite v3, which works like mod_rewrite, so this should work for you.

命名map_company.txt样图文件

Sample map file named map_company.txt

some-company 12
another-one 3423
freds-fill-dirt-and-croissants 44

重写规则:

RewriteMap map_company txt:map_company.txt [NC] 

RewriteCond ${map_company:$1|NOT_FOUND} !NOT_FOUND
RewriteRule ^/(.*) /listing.php?company_id=${map_company:$1} [NC,QSA,L]

RewriteMap指令分配map_company.txt文本文件名为变量map_company(名称相同只是要一致)。

RewriteMap assigns the map_company.txt text file to a variable named map_company (named the same just to be consistent).

重写规则正在做的工作。它的斜线到 $ 1 ,然后重写你的listing.php网址后捕获的一切。该 $ {map_company:$ 1} 正在上升中的URL映射文件,并返回ID

RewriteRule is doing the work. It captures everything after the slash into $1, then rewrites it to your listing.php url. The ${map_company:$1} is looking up the url in the map file, and returning the id.

的RewriteCond只是做了仔细检查,看看是否上市是存在的。 $ 1是从重写规则URL来临,NOT_FOUND是默认值,如果没有找到,条件是,如果它不是,NOT_FOUND(有点纠结 - 但它只是检查它是否在文件中)。如果它是在该文件中,则重写规则将运行。如果它不是在文件中,它跳过了重写规则,并落空至更多的规则(也许是一个网页未找到或其他默认设置)。

RewriteCond is just doing a double-check to see if the listing is there. The $1 is coming from the RewriteRule url, the NOT_FOUND is a default value if not found, and the condition is if it's not-NOT_FOUND (a little tangled - but it's just checking if it's in the file). If it's in the file, the RewriteRule will be run. If it's not in the file, it skips the RewriteRule, and falls through to more rules (perhaps to a page-not-found or some other default).

 
精彩推荐