1

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)

As I really don't know how to achieve this job I have no code example, sorry.

  • Does it have to be done in .htaccess? For something this complicated it probably makes sense to pass the airports/italy/rome/aeroporto-di-roma-fiumicino into your web app and have it deal with the conversion. – Stephen Ostermiller May 24 '17 at 14:02
  • 1
    How many substitutions do you have to deal with? Is it just three codes (IT, ROM, and FCO)? Are the dozens? Hundreds? – Stephen Ostermiller May 24 '17 at 14:02
  • I have 2 versions of the code parameter, the first is code=IT-ROM which shows all airports of Rome and there are about 9K cities... the full version is code=IT-ROM-FCO and shows data of a specified airport...about 16K of them. I think I'm going to insert another parameter called alias in the URL so I can retrieve it in .htaccess and exclude the codes – Cliff Burton May 24 '17 at 14:46
  • 25,000 substitutions is far too many to specify in .htaccess. It won't perform well enough. – Stephen Ostermiller May 24 '17 at 14:48
  • 1
    Conversion in .htaccess has to go the other way around. You get the pretty URL coming in and you have to convert into the codes that the web app uses. If you have the codes in URL, you don't need to pass aliases to the web app at all, you can just ignore them. – Stephen Ostermiller May 24 '17 at 15:01
2

I'd recommend keeping the codes in the URL. There are some advantages to do so:

  • Codes are easy to keep static. Names of places often change over time. If the codes are there you don't have to keep records of old names and implement redirects based on old names.
  • It is easy to rewrite when the data you need for the web-app is in the URL. .htaccess is not great for lookup tables. Putting 25,000 rewrite rules into .htaccess would be a performance killer.

I'd recommend using a URL like: /IT-ROM-FCO/airports/italy/rome/aeroporto-di-roma-fiumicino You would use the following rewrite rule to put the codes into the parameter and remove the slug:

RewriteRule ^/?([A-Z]+-[A-Z]+(-[A-Z]+)?).* /demo.html?code=$1 [L]

That would take any URL that has two or three abbreviations at the beginning and put them into the code parameter. That should support either your code=IT-ROM or code=IT-ROM-FCO cases you mentioned in the comments.

Then when you generate URLs on your site, put the pretty version in the hrefs:

<a href="/IT-ROM-FCO/airports/italy/rome/aeroporto-di-roma-fiumicino">

You'll also need to "canonicalize" your URLs. The URL /IT-ROM-FCO/foo-bar will then show the same content. It is probably easiest to solve this by using a meta link rel canonical tag in the head of the page with the pretty URL. That will tell Google which version is your preferred URL.

<link rel="canonical" href="https://example.com/IT-ROM-FCO/airports/italy/rome/aeroporto-di-roma-fiumicino">
  • Thank you so much @Stephen!! Tomorrow I will talk with SEO specialist and implement your solution! – Cliff Burton May 24 '17 at 15:18
  • 1
    You might also want to consider lowercasing the codes. Mixed case URLs don't always work very well. Rather, they work fine, but some software tries to lowercase the whole URL. Crawlers and content management systems, mostly. That can cause lots of crawl errors. – Stephen Ostermiller May 24 '17 at 15:27
  • Sorry Stephen but it seems I can't make it work. The full URL I have is http://webpartner.it/orariovoli/aeroporti.html?code=IT-ROM-FCO and should be changed to http://webpartner.it/orariovoli/aeroporti/fco/aeroporto-di-roma-fiumicino (new URL asked by SEO specialist, but it doesn't work with the older version) – Cliff Burton May 29 '17 at 12:50
  • One thing to note: the site I am working on is made with Joomla! and I suppose something is interferring with my htaccess – Cliff Burton May 29 '17 at 12:52
  • Rewrite rules won't prevent old URLs from working. They just make new URLs work in addition. The easiest way to prevent Google from using the old URLs is to implement the canonical tags. – Stephen Ostermiller May 30 '17 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.