redirection.html 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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/ or dest.example.com/script.php, <b>sometime you might need to add tailing slash (/) to your URL depending on your use cases</b></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. <br><br>
  74. </div>
  75. </div>
  76. </div>
  77. <script>
  78. /*
  79. Redirection functions
  80. */
  81. $(".checkbox").checkbox();
  82. function resetForm() {
  83. document.getElementById("rurl").value = "";
  84. document.getElementsByName("destination-url")[0].value = "";
  85. document.getElementsByName("forward-childpath")[0].checked = true;
  86. }
  87. function addRules(){
  88. let redirectUrl = document.querySelector('input[name="redirection-url"]').value;
  89. let destUrl = document.querySelector('input[name="destination-url"]').value;
  90. let forwardChildpath = document.querySelector('input[name="forward-childpath"]').checked;
  91. let redirectType = document.querySelector('input[name="redirect-type"]:checked').value;
  92. $.ajax({
  93. url: "/api/redirect/add",
  94. method: "POST",
  95. data: {
  96. redirectUrl: redirectUrl,
  97. destUrl: destUrl,
  98. forwardChildpath: forwardChildpath,
  99. redirectType: parseInt(redirectType),
  100. },
  101. success: function(data){
  102. if (data.error != undefined){
  103. alert(data.error);
  104. }else{
  105. $("#ruleAddSucc").stop().finish().slideDown("fast").delay(3000).slideUp("fast");
  106. }
  107. initRedirectionRuleList();
  108. resetForm();
  109. }
  110. });
  111. }
  112. function deleteRule(obj){
  113. let targetURL = $(obj).attr("rurl");
  114. targetURL = JSON.parse(decodeURIComponent(targetURL));
  115. if (confirm("Confirm remove redirection from " + targetURL + " ?")){
  116. $.ajax({
  117. url: "/api/redirect/delete",
  118. method: "POST",
  119. data: {
  120. redirectUrl: targetURL,
  121. },
  122. success: function(data){
  123. if (data.error != undefined){
  124. alert(data.error);
  125. }else{
  126. $("#delRuleSucc").stop().finish().slideDown("fast").delay(3000).slideUp("fast");
  127. }
  128. initRedirectionRuleList();
  129. }
  130. });
  131. }
  132. }
  133. function initRedirectionRuleList(){
  134. $("#redirectionRuleList").html("");
  135. $.get("/api/redirect/list", function(data){
  136. data.forEach(function(entry){
  137. $("#redirectionRuleList").append(`<tr>
  138. <td><a href="${entry.RedirectURL}" target="_blank">${entry.RedirectURL}</a></td>
  139. <td>${entry.TargetURL}</td>
  140. <td>${entry.ForwardChildpath?"<i class='ui green checkmark icon'></i>":"<i class='ui red remove icon'></i>"}</td>
  141. <td>${entry.StatusCode==307?"Temporary Redirect (307)":"Moved Permanently (301)"}</td>
  142. <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>
  143. </tr>`);
  144. });
  145. if (data.length == 0){
  146. $("#redirectionRuleList").append(`<tr colspan="4"><td><i class="green check circle icon"></i> No redirection rule</td></tr>`);
  147. }
  148. });
  149. }
  150. initRedirectionRuleList();
  151. $("#rurl").on('change', (event) => {
  152. const value = event.target.value.trim().replace(/^(https?:\/\/)/, '');
  153. event.target.value = value;
  154. });
  155. </script>