简体   繁体   English

.htaccess从代码中打印“漂亮” URL

[英].htaccess print “pretty” urls from code

Yes, I know, there are tons of questions like this, but I can't find one which solves my problem so at cost of receiving downvotes I'm going to ask this again. 是的,我知道,有很多这样的问题,但是我找不到能解决我问题的问题,因此我不得不再次提出这个问题,但要花掉我的票数。 Even a duplicate link may help me. 甚至重复的链接也可能对我有帮助。

This is a job that I've never done before, I have this kind of URL demo.html?code=IT-ROM-FCO where: 这是我从未做过的工作,我有这种URL demo.html?code=IT-ROM-FCO ,其中:

  • IT is the IATA code for "Italy" IT“意大利”的IATA代码
  • ROM is the IATA code for "Rome" ROM“罗马”的IATA代码
  • FCO is the IATA code for "Aeroporto di Roma Fiumicino" . FCO“ Aeroporto di Roma Fiumicino”的IATA代码。

I have to rewrite the above URL to display like this airports/italy/rome/aeroporto-di-roma-fiumicino , the strings for the result are coming from an API service. 我必须重写上面的URL才能显示这样的airports/italy/rome/aeroporto-di-roma-fiumicino ,结果的字符串来自API服务。

I know the simple rewrite to change products.php?id=7 to products/7 but I never done such task (get an URL - retrieve some data in PHP w/ the given code - "pretty" print the new url) 我知道将products.php?id=7更改为products/7的简单重写,但是我从未完成过这样的任务(获取URL-使用给定代码在PHP中检索一些数据-“漂亮”打印新的URL)

I don't think this is a job for .htaccess. 我认为这不是.htaccess的工作。 mod_rewrite needs to do its work before PHP is even invoked. mod_rewrite需要在甚至调用PHP之前完成其工作。

Send the request to PHP, but then instead of rendering HTML build the "pretty" URL you want and respond with a redirect header to that URL. 将请求发送到PHP,但不要渲染HTML,而是构建所需的“漂亮” URL,并使用指向该URL的重定向标头进行响应。

If you absolutely need to do this with a rewrite, you might consider building a static map of codes -> URLs and having mod_rewrite consult that map. 如果您绝对需要通过重写来执行此操作,则可以考虑构建代码的静态映射-> URL,并让mod_rewrite查阅该映射。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM