Forum Moderators: phranque

Message Too Old, No Replies

Redirect Map and Key File with Multiple URI/abbreviations

         

rraammss78

1:19 am on Apr 15, 2015 (gmt 0)

10+ Year Member



Hello ,

I am trying a redirection scenario for which I need your guidance.

**Original URLs**



- https://www.example.com/books -- single URI

- https://www.example.com/books/author -- Double URI

- https://www.example.com/cd/rock/west - Triple URI

**Redirected URLs:**


- https://www.example.net/newbooks -- single URI

- https://www.example.net/newbooks/newauthor -- Double URI



- https://www.example.net/newcd/newrock/newwest - Triple URI

For the above given URLs, redirection must be applied using rewrite Map.

Current key value pair are in keys.txt file like this.


----------


- **books** https://www.example.net/newbooks
- **books/author** https://www.example.net/newbooks/newauthor
- **cd/jazz/western** https://www.example.net/newcd/newrock/newwest


----------


**Current Solution only handles single URI pattern, handles any case in the URI **

- RewriteMap lowercase int:tolower
- RewriteMap mymap txt:/folderstructure/keys.txt
- RewriteCond %{REQUEST_URI} ^/([A-Za-z0-9]+)/?$ [NC]
- RewriteRule ^/(.*) ${mymap:${lowercase:%1}} [L,R=301]

**Question :** How to extract multiple URI values between "/" (forward slashes in the REQUEST_URI) values and contact into a single key to be supplied as argement to ${mymap:${lowercase:?}}. It is must to call the tolower function, for the reason that URI in the original URL may come in mixedcase.

**For example :**
If the incoming URL is https://www.example.com/BooKS/AuThOr (mixedcase)
the URI value (BooKS/AuThOr) must be converted to lowercase and the final key for look must be "books/author" in the rewrite rule.


**The below piece of code is not robust, but it solved the problem temporarily **

keys.txt


----------


- books https://www.example.net/newbooks
- author https://www.example.net/newbooks/newauthor
- western https://www.example.net/newcd/newrock/newwest


----------


- RewriteCond %{REQUEST_URI} ^/books/([A-Za-z0-9]+)/?$ [NC]
- RewriteRule ^/(.*) ${mymap:${lowercase:%1}} [L,R=301]


----------


- RewriteCond %{REQUEST_URI} ^/cd/rock/([A-Za-z0-9]+)/?$ [NC]
- RewriteRule ^/(.*) ${mymap:${lowercase:%1}} [L,R=301]

Thanks.

[edited by: phranque at 1:45 am (utc) on Apr 15, 2015]
[edit reason] Please Use example.com [webmasterworld.com] [/edit]

phranque

1:51 am on Apr 15, 2015 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld, rraammss78!


i think you just want to add a slash to your capture group specification in the regular expression:
^/([A-Za-z0-9/]+)/?$

rraammss78

1:58 am on Apr 15, 2015 (gmt 0)

10+ Year Member



The Original URLs may have the trailing "/" that why its added as optional.

For eg : [example.com...] and
[example.com...]

are valid.

Thanks.

phranque

2:12 am on Apr 15, 2015 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



the final key for look must be "books/author" in the rewrite rule


if you want to capture "books/author" you must add a slash to your capture group specification in the regular expression.

rraammss78

2:16 am on Apr 15, 2015 (gmt 0)

10+ Year Member



The pattern must be generic to capture,

/books -- single URI

- /books/author -- Double URI

- /cd/rock/west - Triple URI


which the above solution does not address it.

Thank you for your response.

lucy24

3:37 am on Apr 15, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What's the problem with
^/((?:\w+/?){1,3})$
?
which the above solution does not address it.

Yes, it does. You may not have noticed that phranque added / to the permitted items in your group. My version is more complicated because it only permits 1, 2 or 3 directory layers. (You might think it also offers the risk of breaking up "lotsabooks" into three pieces, like "lots" "obo" "oks" but regular expressions simply don't behave that way. Besides, it wouldn't matter in this situation.) To make up for it I've simplified [A-Za-z0-9] to \w because I can't imagine it makes any difference whether you allow or exclude _ (lowline counts as \w) in the search.