redirection.html 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. <!-- Current list of redirection rules-->
  7. <div style="width: 100%; overflow-x: auto;">
  8. <table class="ui sortable unstackable celled table" >
  9. <thead>
  10. <tr>
  11. <th>Redirection URL</th>
  12. <th>Destination URL</th>
  13. <th class="no-sort">Copy Pathname</th>
  14. <th class="no-sort">Status Code</th>
  15. <th class="no-sort">Remove</th>
  16. </tr>
  17. </thead>
  18. <tbody id="redirectionRuleList">
  19. <tr>
  20. <td></td>
  21. <td></td>
  22. <td></td>
  23. <td></td>
  24. </tr>
  25. </tbody>
  26. </table>
  27. </div>
  28. <div class="ui green message" id="delRuleSucc" style="display:none;">
  29. <i class="ui green checkmark icon"></i> Redirection Rule Deleted
  30. </div>
  31. <!-- Options -->
  32. <div class="ui basic segment advanceoptions">
  33. <div class="ui accordion advanceSettings">
  34. <div class="title">
  35. <i class="dropdown icon"></i>
  36. Advance Settings
  37. </div>
  38. <div class="content">
  39. <div class="ui basic segment">
  40. <div class="ui toggle checkbox">
  41. <input id="redirectRegex" type="checkbox">
  42. <label>Enable Regular Expression Support<br>
  43. <small>Regular expression redirection check will noticeably slow down page load<br>
  44. Support <a href="https://yourbasic.org/golang/regexp-cheat-sheet/" target="_blank">Go style regex</a>. e.g. <code style="background-color: rgb(44, 44, 44); color: white">.\.redirect\.example\.com</code></small></label>
  45. </div>
  46. </div>
  47. </div>
  48. </div>
  49. </div>
  50. <!-- Add New Redirection Rules -->
  51. <div class="ui divider"></div>
  52. <h4>Add Redirection Rule</h4>
  53. <div class="ui form">
  54. <div class="field">
  55. <label>Redirection URL (From)</label>
  56. <input type="text" id="rurl" name="redirection-url" placeholder="Redirection URL">
  57. <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>
  58. </div>
  59. <div class="field">
  60. <label>Destination URL (To)</label>
  61. <input type="text" name="destination-url" placeholder="Destination URL">
  62. <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>
  63. </div>
  64. <div class="field">
  65. <div class="ui checkbox">
  66. <input type="checkbox" name="forward-childpath" tabindex="0" class="hidden" checked>
  67. <label>Forward Pathname</label>
  68. </div>
  69. <div class="ui message">
  70. <p>Append the current pathname after the redirect destination</p>
  71. <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>
  72. <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
  73. </div>
  74. </div>
  75. <div class="grouped fields">
  76. <label>Redirection Status Code</label>
  77. <div class="field">
  78. <div class="ui radio checkbox">
  79. <input type="radio" name="redirect-type" value="307" checked>
  80. <label>Temporary Redirect <br><small>Status Code: 307</small></label>
  81. </div>
  82. </div>
  83. <div class="field">
  84. <div class="ui radio checkbox">
  85. <input type="radio" name="redirect-type" value="301">
  86. <label>Moved Permanently <br><small>Status Code: 301</small></label>
  87. </div>
  88. </div>
  89. </div>
  90. <button class="ui basic button" onclick="addRules();"><i class="ui teal plus icon"></i> Add Redirection Rule</button>
  91. <div class="ui green message" id="ruleAddSucc" style="display:none;">
  92. <i class="ui green checkmark icon"></i> Redirection Rules Added
  93. </div>
  94. <br><br>
  95. </div>
  96. </div>
  97. </div>
  98. <script>
  99. /*
  100. Redirection functions
  101. */
  102. $(".checkbox").checkbox();
  103. $(".advanceSettings").accordion();
  104. function resetForm() {
  105. document.getElementById("rurl").value = "";
  106. document.getElementsByName("destination-url")[0].value = "";
  107. document.getElementsByName("forward-childpath")[0].checked = true;
  108. }
  109. function addRules(){
  110. let redirectUrl = document.querySelector('input[name="redirection-url"]').value;
  111. let destUrl = document.querySelector('input[name="destination-url"]').value;
  112. let forwardChildpath = document.querySelector('input[name="forward-childpath"]').checked;
  113. let redirectType = document.querySelector('input[name="redirect-type"]:checked').value;
  114. $.cjax({
  115. url: "/api/redirect/add",
  116. method: "POST",
  117. data: {
  118. redirectUrl: redirectUrl,
  119. destUrl: destUrl,
  120. forwardChildpath: forwardChildpath,
  121. redirectType: parseInt(redirectType),
  122. },
  123. success: function(data){
  124. if (data.error != undefined){
  125. alert(data.error);
  126. }else{
  127. $("#ruleAddSucc").stop().finish().slideDown("fast").delay(3000).slideUp("fast");
  128. }
  129. initRedirectionRuleList();
  130. resetForm();
  131. }
  132. });
  133. }
  134. function deleteRule(obj){
  135. let targetURL = $(obj).attr("rurl");
  136. targetURL = JSON.parse(decodeURIComponent(targetURL));
  137. if (confirm("Confirm remove redirection from " + targetURL + " ?")){
  138. $.cjax({
  139. url: "/api/redirect/delete",
  140. method: "POST",
  141. data: {
  142. redirectUrl: targetURL,
  143. },
  144. success: function(data){
  145. if (data.error != undefined){
  146. alert(data.error);
  147. }else{
  148. $("#delRuleSucc").stop().finish().slideDown("fast").delay(3000).slideUp("fast");
  149. }
  150. initRedirectionRuleList();
  151. }
  152. });
  153. }
  154. }
  155. function initRedirectionRuleList(){
  156. $("#redirectionRuleList").html("");
  157. $.get("/api/redirect/list", function(data){
  158. data.forEach(function(entry){
  159. $("#redirectionRuleList").append(`<tr>
  160. <td><a href="${entry.RedirectURL}" target="_blank">${entry.RedirectURL}</a></td>
  161. <td>${entry.TargetURL}</td>
  162. <td>${entry.ForwardChildpath?"<i class='ui green checkmark icon'></i>":"<i class='ui red remove icon'></i>"}</td>
  163. <td>${entry.StatusCode==307?"Temporary Redirect (307)":"Moved Permanently (301)"}</td>
  164. <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>
  165. </tr>`);
  166. });
  167. if (data.length == 0){
  168. $("#redirectionRuleList").append(`<tr colspan="4"><td><i class="green check circle icon"></i> No redirection rule</td></tr>`);
  169. }
  170. });
  171. }
  172. initRedirectionRuleList();
  173. function initRegexpSupportToggle(){
  174. $.get("/api/redirect/regex", function(data){
  175. //Set the checkbox initial state
  176. if (data == true){
  177. $("#redirectRegex").parent().checkbox("set checked");
  178. }else{
  179. $("#redirectRegex").parent().checkbox("set unchecked");
  180. }
  181. //Bind event to the checkbox
  182. $("#redirectRegex").on("change", function(){
  183. $.cjax({
  184. url: "/api/redirect/regex",
  185. method: "POST",
  186. data: {"enable": $(this)[0].checked},
  187. success: function(data){
  188. if (data.error != undefined){
  189. msgbox(data.error, false);
  190. }else{
  191. msgbox("Regex redirect setting updated", true);
  192. }
  193. }
  194. });
  195. });
  196. });
  197. }
  198. initRegexpSupportToggle();
  199. $("#rurl").on('change', (event) => {
  200. const value = event.target.value.trim().replace(/^(https?:\/\/)/, '');
  201. event.target.value = value;
  202. });
  203. </script>