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