0

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:

  • IT is the IATA code for "Italy"
  • ROM is the IATA code for "Rome"
  • FCO is the IATA code for "Aeroporto di Roma Fiumicino".

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.

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)

Cliff Burton
  • 3,414
  • 20
  • 33
  • 47
  • If you tried something that may have failed, maybe you should include it in your question. – Funk Forty Niner May 17 '17 at 14:52
  • No @Fred-ii-, I really don't know how to accomplish this task, I'm so frustrated – Cliff Burton May 17 '17 at 14:55
  • Have you seen http://stackoverflow.com/questions/25080835/pretty-urls-with-htaccess --- http://stackoverflow.com/questions/812571/how-to-create-friendly-url-in-php --- https://code.tutsplus.com/tutorials/using-htaccess-files-for-pretty-urls--net-6049 - to name a few. – Funk Forty Niner May 17 '17 at 14:57
  • [*"So if I do this will search engines recognize my urls and indicize them?"* - So this goes further then, as in "SEO"](http://stackoverflow.com/questions/44028206/htaccess-print-pretty-urls-from-code#comment75083135_44028313). I think the question's already getting too broad now. – Funk Forty Niner May 17 '17 at 14:57
  • @Fred-ii- My job is to return that kind of URL, then someone else in my office will do something with a Joomla component called sh404SEF to do "SEO things" ...I'm a programmer, SEO is a nightmare for me. – Cliff Burton May 17 '17 at 15:02
  • SEO questions should be asked on the [Pro Webmasters](https://webmasters.stackexchange.com/questions/tagged/seo), tagged with [seo](http://stackoverflow.com/questions/tagged/seo). See also https://meta.stackexchange.com/questions/14056/seo-in-stack-overflow – Funk Forty Niner May 17 '17 at 15:05
  • I know @Fred-ii-, but this regards a programming problem too as my script works with that codes, my boss wants that URL to be "pretty" I think it is not _strictly_ related to SEO. This is the situation I am in: the script needs that codes - boss asks to "pretty print" URL - SEO specialist will do her magic later – Cliff Burton May 17 '17 at 15:11

1 Answers1

1

I don't think this is a job for .htaccess. mod_rewrite needs to do its work before PHP is even invoked.

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.

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.

pjmorse
  • 9,204
  • 9
  • 54
  • 124
  • So if I do this will search engines recognize my urls and indicize them? – Cliff Burton May 17 '17 at 14:56
  • If you return a redirect to a search engine spider it should recognize that as the canonical URL and index that address, yes. – pjmorse May 17 '17 at 14:58
  • @Fred-ii- OP has a need to get a request with a defined URL pattern and return both data and a differently-formatted URL. The function requestURL -> prettyURL requires an API query. I am suggesting a way to make that happen. How does that *not* help? – pjmorse May 17 '17 at 15:00
  • Now that I'm thinking about it... the URL `airports/italy/rome/aeroporto-di-roma-fiumicino` doesn't exists, I can't do that! My scripts do need that codes "IT-ROM-FCO" to take data from API provder and print a result – Cliff Burton May 17 '17 at 15:05