2

So here’s what I’m trying to accomplish. I have this link, I want to 301 redirect:

Origin URL:

https://www.example.com/folder1/page.aspx?fh_location=%2f%2fpackages%2fnl_BE%2fpr_earlybookable%3E%7b0%7d%2fpk_active%3d1%2fpk_siteswitch%3E%7bnvb%7d%2fpk_country%3E%7biso_pt%7d%2fpk_city%3E%7bgeo_9833%7d%2fpr_duration%3E%7b0608%7d%2fpk_hotelonly%3d0&fh_sort_by=pr_defaultprice&fh_secondid=nbs_city_cli16a_s18&fh_view=detail&fh_eds=%C3%9F&RoomOccupation1=CLI16A_2a_LO_2_0

Condition 1: I have a list of Directories such as folder1, folder2, folder3, folder4. I want to redirect only if the URL contains any of the folders listed above, but not for folder5 or any other folders not listed above in the URL.

Condition 2: Now comes the tricky part; in the destination it should take the id cli16a from the URL fh_secondid=nbs_city_cli16a_s18 and then append to the destination URL as seen below

Condition 3: Now the most tricky part. Add that Y to the destination URL, which represents the folder in the origin URL, eg., folder1 = Y, folder2 = O, folder3 = B, folder4 = C.

So far I have come up with the below.

RewriteMap WE NEED TO GET A MAPPING OF TYPE OF HOTEL (not sure what to put here)
RewriteCond %{REQUEST_URI}  ==(folder1|folder2|folder3|folder4) [NC]
RewriteCond ${conv-lc:%{QUERY_STRING}} (fh_secondid)=[^_]+_[^_]+_([^_]+)_
RewriteRule ^/nl/_USE RewriteMap Matching text here/ ( not sure what to put here)
MrWhite
  • 12,647
  • 4
  • 29
  • 41
Ja Hi
  • 23
  • 2
  • In the param value "nbs_city_cli16a_s18", is the "nbs_city" part constant, or is the required ID always the 3rd part (separated by underscores), which seems to be the method used in your code sample? Is this always lowercase? Should the ID in the resulting URL always be uppercase? I notice in your original URL that the ID seems to appear twice, the second occurance is already uppercase - or is this unreliable? – MrWhite Aug 10 '18 at 14:37
  • 1
    1. The required ID always the 3rd part 2. This is not case sensitive for the resulting URL to work 3. As the resulting url can either be lowercase or uppercase, we can take the first occurrence which is the 3rd part (separated by underscores) – Ja Hi Aug 12 '18 at 19:07

1 Answers1

0

By the sounds of it, you don't necessarily need a RewriteMap, providing the number of directories is limited, which they appear to be, and the ID is not case-sensitive in the target URL, which you say is not (in comments).

For the ID suffix ("Y", "O", "B" or "C") we can create an environment variable, based on the directory being requested:

RewriteEngine On

# Assign value to ID_SUFFIX based on the directory being accessed
RewriteRule ^/?folder1 - [E=ID_SUFFIX:Y]
RewriteRule ^/?folder2 - [E=ID_SUFFIX:O]
RewriteRule ^/?folder3 - [E=ID_SUFFIX:B]
RewriteRule ^/?folder4 - [E=ID_SUFFIX:C]

Then, only process the request further if the ID_SUFFIX env var has been set, capturing the required ID from the query string:

# Capture ID and redirect
RewriteCond %{ENV:ID_SUFFIX} .
REwriteCond %{QUERY_STRING} fh_secondid=[^_]+_[^_]+_([^_]+)
RewriteRule ^ /be/_%1%{ENV:ID_SUFFIX}/ [R=302,L]

The expression %{ENV:ID_SUFFIX} naturally refers to the env var set earlier (if at all). Initially, we simply check that this is set (ie. contains something) - it is only set if one of the named directories has been requested.

%1 is a backreference to the first captured group in the last matched CondPattern. ie. This holds the ID (eg. cli16a in your example).

This then constructs a substitution of the form /be/_cli16aY/ and issues an external redirect.

Note that this is currently a 302 (temporary) redirect. Only change it to a 301 (permanent) when you are sure it's working OK, in order to avoid potenatial caching issues.

MrWhite
  • 12,647
  • 4
  • 29
  • 41