redirection.html 6.5 KB

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