.autoRedirect 3.79 KB
Newer Older
Laurent Heirendt's avatar
Laurent Heirendt committed
1
2
<html>
  <head>
Laurent Heirendt's avatar
Laurent Heirendt committed
3
4
5
6
7
8
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script type="text/javascript">
    function UrlExists(url, cb){
        jQuery.ajax({
            url:      url,
            dataType: 'text',
Laurent Heirendt's avatar
Laurent Heirendt committed
9
            type:     'GET',
10
            cache:    false,
Laurent Heirendt's avatar
Laurent Heirendt committed
11
12
13
14
15
16
            complete:  function(xhr){
                if(typeof cb === 'function')
                cb.apply(this, [xhr.status]);
            }
        });
    }
Laurent Heirendt's avatar
Laurent Heirendt committed
17

Jacek Lebioda's avatar
Jacek Lebioda committed
18
    function GetShortcutDestination() {
Laurent Heirendt's avatar
Laurent Heirendt committed
19
20
        var s = window.location.href;
        var pathArray = s.split('?');
21

Jacek Lebioda's avatar
Jacek Lebioda committed
22
        // Cut the query if it exists
Laurent Heirendt's avatar
Laurent Heirendt committed
23
        if (pathArray.length > 1) {
Jacek Lebioda's avatar
Jacek Lebioda committed
24
            return pathArray[1];
25
        } else {
Jacek Lebioda's avatar
Jacek Lebioda committed
26
            return '';
27
        }
Jacek Lebioda's avatar
Jacek Lebioda committed
28
29
    }

30
31
32
33
    function RedirectTo(newLocation) {
        document.location.replace(newLocation);
    }

Jacek Lebioda's avatar
Jacek Lebioda committed
34
35
    // Pick the shortcut link destination from URL, like: `category:subcategory:card-name`
    var sub = GetShortcutDestination();
Laurent Heirendt's avatar
Laurent Heirendt committed
36

Jacek Lebioda's avatar
Jacek Lebioda committed
37
38
39
40
41
42
43
    // Define URLs
    var internalPortalURL = 'https://r3-core.pages.uni.lu/howto-cards-internal';
    var externalPortalURL = 'https://r3.pages.uni.lu/howto-cards';

    // First, check whether internal pages are accessible...
    var internalIndexURL = internalPortalURL + '/index.html';
    UrlExists(internalIndexURL, function(status){
Laurent Heirendt's avatar
Laurent Heirendt committed
44
        if(status === 200){
Jacek Lebioda's avatar
Jacek Lebioda committed
45
            // If the internal pages are accessible, try to redirect into correct page
46
            if (sub.length > 0) {
Jacek Lebioda's avatar
Jacek Lebioda committed
47
48
49
                var internalCardURL = internalPortalURL + '/stable/internal/cards/' + sub;
                UrlExists(internalCardURL, function(status){
                    if (status == 200) { // The sub-card is internal, and is accessible
50
                        RedirectTo(internalCardURL);
Laurent Heirendt's avatar
fix bug    
Laurent Heirendt committed
51
                    } else {
Laurent Heirendt's avatar
Laurent Heirendt committed
52
                        UrlExists(internalPortalURL + '/stable/external/cards/' + sub, function(status){
Laurent Heirendt's avatar
Laurent Heirendt committed
53
                            if (status == 200) { // if sub-card is internal in the external directory and exists
54
                                RedirectTo(internalPortalURL + '/stable/external/cards/' + sub);
Laurent Heirendt's avatar
Laurent Heirendt committed
55
                            } else {
56
                                RedirectTo(internalPortalURL + '/stable/404.html');
Laurent Heirendt's avatar
Laurent Heirendt committed
57
58
                            }
                        });
Laurent Heirendt's avatar
fix bug    
Laurent Heirendt committed
59
                    }
Laurent Heirendt's avatar
add )    
Laurent Heirendt committed
60
                });
Laurent Heirendt's avatar
Laurent Heirendt committed
61
            } else {
Jacek Lebioda's avatar
Jacek Lebioda committed
62
63
64
65
66
                // The user did not request specific card, redirect him just to the stable index
                var internalPortalIndexURL = internalPortalURL + '/stable';
                var internalPortal404URL = internalPortalURL + '/stable/404.html';
                UrlExists(internalPortalIndexURL, function(status){
                    if (status == 200) { // if page is accessible
67
                        RedirectTo(internalPortalIndexURL);
Laurent Heirendt's avatar
Laurent Heirendt committed
68
                    } else {
69
                        RedirectTo(internalPortal404URL);
Laurent Heirendt's avatar
Laurent Heirendt committed
70
71
                    }
                });
72
            }
Laurent Heirendt's avatar
Laurent Heirendt committed
73
        } else {
Jacek Lebioda's avatar
Jacek Lebioda committed
74
75
76
77
78
79
80
            // The internal pages are not accessible, therefore the user is redirected into correct external page
            if (sub.length > 0) {
                sub = 'external/cards/' + sub;
            }
            var externalPortalCardURL = externalPortalURL + '/stable/' + sub;
            var externalPortal404URL = externalPortalURL + '/stable/404.html';
            UrlExists(externalPortalURL + '/stable/' + sub, function(status){
Jacek Lebioda's avatar
Jacek Lebioda committed
81
                if (status == 200) { // if sub-card is internal and exists
82
                    RedirectTo(externalPortalCardURL);
Laurent Heirendt's avatar
Laurent Heirendt committed
83
                } else {
84
                    RedirectTo(externalPortal404URL);
Laurent Heirendt's avatar
Laurent Heirendt committed
85
86
                }
            });
Laurent Heirendt's avatar
Laurent Heirendt committed
87
88
89
        }
    });
    </script>
Laurent Heirendt's avatar
Laurent Heirendt committed
90
91
92
  </head>
  <body>
    <p>
93
      Please follow to <a href="https://r3.pages.uni.lu/howto-cards/stable">https://r3.pages.uni.lu/howto-cards/stable</a>!
Laurent Heirendt's avatar
Laurent Heirendt committed
94
95
96
    </p>
  </body>
</html>