redirection.html 7.6 KB

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