redirection.html 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <div class="standardContainer">
  2. <div class="ui basic segment">
  3. <h2>Redirection Rules</h2>
  4. <p>Add exception case for redirecting any matching URLs</p>
  5. </div>
  6. <div style="width: 100%; overflow-x: auto;">
  7. <table class="ui sortable unstackable celled table" >
  8. <thead>
  9. <tr>
  10. <th>Redirection URL</th>
  11. <th>Destination URL</th>
  12. <th class="no-sort">Copy Pathname</th>
  13. <th class="no-sort">Status Code</th>
  14. <th class="no-sort">Remove</th>
  15. </tr>
  16. </thead>
  17. <tbody id="redirectionRuleList">
  18. <tr>
  19. <td></td>
  20. <td></td>
  21. <td></td>
  22. <td></td>
  23. </tr>
  24. </tbody>
  25. </table>
  26. </div>
  27. <div class="ui green message" id="delRuleSucc" style="display:none;">
  28. <i class="ui green checkmark icon"></i> Redirection Rule Deleted
  29. </div>
  30. <div class="ui divider"></div>
  31. <h4>Add Redirection Rule</h4>
  32. <div class="ui form">
  33. <div class="field">
  34. <label>Redirection URL (From)</label>
  35. <input type="text" id="rurl" name="redirection-url" placeholder="Redirection URL">
  36. <small><i class="ui circle info icon"></i> Any matching prefix of the request URL will be redirected to the destination URL, e.g. redirect.example.com</small>
  37. </div>
  38. <div class="field">
  39. <label>Destination URL (To)</label>
  40. <input type="text" name="destination-url" placeholder="Destination URL">
  41. <small><i class="ui circle info icon"></i> The target URL request being redirected to, e.g. dest.example.com/mysite</small>
  42. </div>
  43. <div class="field">
  44. <div class="ui checkbox">
  45. <input type="checkbox" name="forward-childpath" tabindex="0" class="hidden" checked>
  46. <label>Forward Pathname</label>
  47. </div>
  48. <div class="ui message">
  49. <p>Append the current pathname after the redirect destination</p>
  50. <i class="check square outline icon"></i> old.example.com<b>/blog?post=13</b> <i class="long arrow alternate right icon" style="margin-left: 1em;"></i> new.example.com<b>/blog?post=13</b> <br>
  51. <i class="square outline icon"></i> old.example.com<b>/blog?post=13</b> <i class="long arrow alternate right icon" style="margin-left: 1em;"></i> new.example.com
  52. </div>
  53. </div>
  54. <div class="grouped fields">
  55. <label>Redirection Status Code</label>
  56. <div class="field">
  57. <div class="ui radio checkbox">
  58. <input type="radio" name="redirect-type" value="307" checked>
  59. <label>Temporary Redirect <br><small>Status Code: 307</small></label>
  60. </div>
  61. </div>
  62. <div class="field">
  63. <div class="ui radio checkbox">
  64. <input type="radio" name="redirect-type" value="301">
  65. <label>Moved Permanently <br><small>Status Code: 301</small></label>
  66. </div>
  67. </div>
  68. </div>
  69. <button class="ui basic button" onclick="addRules();"><i class="ui teal plus icon"></i> Add Redirection Rule</button>
  70. <div class="ui green message" id="ruleAddSucc" style="display:none;">
  71. <i class="ui green checkmark icon"></i> Redirection Rules Added
  72. </div>
  73. </div>
  74. </div>
  75. <script>
  76. $(".checkbox").checkbox();
  77. function resetForm() {
  78. document.getElementById("rurl").value = "";
  79. document.getElementsByName("destination-url")[0].value = "";
  80. document.getElementsByName("forward-childpath")[0].checked = true;
  81. }
  82. function addRules(){
  83. let redirectUrl = document.querySelector('input[name="redirection-url"]').value;
  84. let destUrl = document.querySelector('input[name="destination-url"]').value;
  85. let forwardChildpath = document.querySelector('input[name="forward-childpath"]').checked;
  86. let redirectType = document.querySelector('input[name="redirect-type"]:checked').value;
  87. $.ajax({
  88. url: "/api/redirect/add",
  89. method: "POST",
  90. data: {
  91. redirectUrl: redirectUrl,
  92. destUrl: destUrl,
  93. forwardChildpath: forwardChildpath,
  94. redirectType: parseInt(redirectType),
  95. },
  96. success: function(data){
  97. if (data.error != undefined){
  98. alert(data.error);
  99. }else{
  100. $("#ruleAddSucc").stop().finish().slideDown("fast").delay(3000).slideUp("fast");
  101. }
  102. initRedirectionRuleList();
  103. }
  104. });
  105. }
  106. function deleteRule(obj){
  107. let targetURL = $(obj).attr("rurl");
  108. targetURL = JSON.parse(decodeURIComponent(targetURL));
  109. if (confirm("Confirm remove redirection from " + targetURL + " ?")){
  110. $.ajax({
  111. url: "/api/redirect/delete",
  112. method: "POST",
  113. data: {
  114. redirectUrl: targetURL,
  115. },
  116. success: function(data){
  117. if (data.error != undefined){
  118. alert(data.error);
  119. }else{
  120. $("#delRuleSucc").stop().finish().slideDown("fast").delay(3000).slideUp("fast");
  121. }
  122. initRedirectionRuleList();
  123. }
  124. });
  125. }
  126. }
  127. function initRedirectionRuleList(){
  128. $("#redirectionRuleList").html("");
  129. $.get("/api/redirect/list", function(data){
  130. data.forEach(function(entry){
  131. $("#redirectionRuleList").append(`<tr>
  132. <td>${entry.RedirectURL} </td>
  133. <td>${entry.TargetURL}</td>
  134. <td>${entry.ForwardChildpath?"<i class='ui green checkmark icon'></i>":"<i class='ui red remove icon'></i>"}</td>
  135. <td>${entry.StatusCode==307?"Temporary Redirect (307)":"Moved Permanently (301)"}</td>
  136. <td><button onclick="deleteRule(this);" rurl="${encodeURIComponent(JSON.stringify(entry.RedirectURL))}" title="Delete redirection rule" class="ui mini red icon basic button"><i class="trash icon"></i></button></td>
  137. </tr>`);
  138. });
  139. if (data.length == 0){
  140. $("#redirectionRuleList").append(`<tr colspan="4"><td><i class="checkmark icon"></i> No redirection rule</td></tr>`);
  141. }
  142. });
  143. }
  144. initRedirectionRuleList();
  145. $("#rurl").on('change', (event) => {
  146. const value = event.target.value.trim().replace(/^(https?:\/\/)/, '');
  147. event.target.value = value;
  148. });
  149. </script>