Welcome to WebmasterWorld Guest from 34.204.173.36

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

Redirect Map and Key File with Multiple URI/abbreviations

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

New User

joined:Apr 15, 2015
posts: 3
votes: 0


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]

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

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11875
votes: 246


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/]+)/?$
1:58 am on Apr 15, 2015 (gmt 0)

New User

joined:Apr 15, 2015
posts: 3
votes: 0


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

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

are valid.

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

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11875
votes: 246


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.
2:16 am on Apr 15, 2015 (gmt 0)

New User

joined:Apr 15, 2015
posts: 3
votes: 0


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.
3:37 am on Apr 15, 2015 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15943
votes: 890


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.