redirection.html 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. <br><br>
  74. <!--
  75. <div class="advancezone ui basic segment">
  76. <div class="ui accordion advanceSettings">
  77. <div class="title">
  78. <i class="dropdown icon"></i>
  79. Advance Options
  80. </div>
  81. <div class="content">
  82. <p>If you need custom header, content or status code other than basic redirects, you can use the advance path rules editor.</p>
  83. <button class="ui black basic button" onclick="createAdvanceRules();"><i class="ui black external icon"></i> Open Advance Rules Editor</button>
  84. </div>
  85. </div>
  86. </div>
  87. -->
  88. </div>
  89. </div>
  90. </div>
  91. <script>
  92. $(".advanceSettings").accordion();
  93. /*
  94. Redirection functions
  95. */
  96. $(".checkbox").checkbox();
  97. function resetForm() {
  98. document.getElementById("rurl").value = "";
  99. document.getElementsByName("destination-url")[0].value = "";
  100. document.getElementsByName("forward-childpath")[0].checked = true;
  101. }
  102. function addRules(){
  103. let redirectUrl = document.querySelector('input[name="redirection-url"]').value;
  104. let destUrl = document.querySelector('input[name="destination-url"]').value;
  105. let forwardChildpath = document.querySelector('input[name="forward-childpath"]').checked;
  106. let redirectType = document.querySelector('input[name="redirect-type"]:checked').value;
  107. $.ajax({
  108. url: "/api/redirect/add",
  109. method: "POST",
  110. data: {
  111. redirectUrl: redirectUrl,
  112. destUrl: destUrl,
  113. forwardChildpath: forwardChildpath,
  114. redirectType: parseInt(redirectType),
  115. },
  116. success: function(data){
  117. if (data.error != undefined){
  118. alert(data.error);
  119. }else{
  120. $("#ruleAddSucc").stop().finish().slideDown("fast").delay(3000).slideUp("fast");
  121. }
  122. initRedirectionRuleList();
  123. }
  124. });
  125. }
  126. function deleteRule(obj){
  127. let targetURL = $(obj).attr("rurl");
  128. targetURL = JSON.parse(decodeURIComponent(targetURL));
  129. if (confirm("Confirm remove redirection from " + targetURL + " ?")){
  130. $.ajax({
  131. url: "/api/redirect/delete",
  132. method: "POST",
  133. data: {
  134. redirectUrl: targetURL,
  135. },
  136. success: function(data){
  137. if (data.error != undefined){
  138. alert(data.error);
  139. }else{
  140. $("#delRuleSucc").stop().finish().slideDown("fast").delay(3000).slideUp("fast");
  141. }
  142. initRedirectionRuleList();
  143. }
  144. });
  145. }
  146. }
  147. function createAdvanceRules(){
  148. showSideWrapper("snippet/advancePathRules.html?t=" + Date.now(), true);
  149. }
  150. function initRedirectionRuleList(){
  151. $("#redirectionRuleList").html("");
  152. $.get("/api/redirect/list", function(data){
  153. data.forEach(function(entry){
  154. $("#redirectionRuleList").append(`<tr>
  155. <td>${entry.RedirectURL} </td>
  156. <td>${entry.TargetURL}</td>
  157. <td>${entry.ForwardChildpath?"<i class='ui green checkmark icon'></i>":"<i class='ui red remove icon'></i>"}</td>
  158. <td>${entry.StatusCode==307?"Temporary Redirect (307)":"Moved Permanently (301)"}</td>
  159. <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>
  160. </tr>`);
  161. });
  162. if (data.length == 0){
  163. $("#redirectionRuleList").append(`<tr colspan="4"><td><i class="checkmark icon"></i> No redirection rule</td></tr>`);
  164. }
  165. });
  166. }
  167. initRedirectionRuleList();
  168. $("#rurl").on('change', (event) => {
  169. const value = event.target.value.trim().replace(/^(https?:\/\/)/, '');
  170. event.target.value = value;
  171. });
  172. </script>