redirection.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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">Actions</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. let encodedEntry = encodeURIComponent(JSON.stringify(entry));
  160. let hrefURL = entry.RedirectURL;
  161. if (!hrefURL.startsWith("http")){
  162. hrefURL = "https://" + hrefURL;
  163. }
  164. $("#redirectionRuleList").append(`<tr>
  165. <td><a href="${hrefURL}" target="_blank">${entry.RedirectURL}</a></td>
  166. <td>${entry.TargetURL}</td>
  167. <td>${entry.ForwardChildpath?"<i class='ui green checkmark icon'></i>":"<i class='ui red remove icon'></i>"}</td>
  168. <td>${entry.StatusCode==307?"Temporary Redirect (307)":"Moved Permanently (301)"}</td>
  169. <td>
  170. <button onclick="editRule(this);" payload="${encodedEntry}" title="Edit redirection rule" class="ui mini circular icon basic button redirectEditBtn"><i class="edit icon"></i></button>
  171. <button onclick="deleteRule(this);" rurl="${encodeURIComponent(JSON.stringify(entry.RedirectURL))}" title="Delete redirection rule" class="ui mini red circular icon basic button"><i class="trash icon"></i></button>
  172. </td>
  173. </tr>`);
  174. });
  175. if (data.length == 0){
  176. $("#redirectionRuleList").append(`<tr><td colspan="5"><i class="green check circle icon"></i> No redirection rule</td></tr>`);
  177. }
  178. });
  179. }
  180. initRedirectionRuleList();
  181. function editRule(obj){
  182. $(".redirectEditBtn").addClass("disabled");
  183. let payload = JSON.parse(decodeURIComponent($(obj).attr("payload")));
  184. let row = $(obj).closest("tr");
  185. let redirectUrl = payload.RedirectURL;
  186. let destUrl = payload.TargetURL;
  187. let forwardChildpath = payload.ForwardChildpath;
  188. let statusCode = payload.StatusCode;
  189. row.html(`
  190. <td>
  191. <div class="ui small input">
  192. <input type="text" value="${redirectUrl}" id="editRedirectUrl">
  193. </div>
  194. </td>
  195. <td>
  196. <div class="ui small input">
  197. <input type="text" value="${destUrl}" id="editDestUrl">
  198. </div>
  199. </td>
  200. <td><div class="ui toggle checkbox"><input type="checkbox" ${forwardChildpath ? "checked" : ""} id="editForwardChildpath"><label></label></div></td>
  201. <td>
  202. <div class="ui radio checkbox"><input type="radio" name="editStatusCode" value="307" ${statusCode == 307 ? "checked" : ""}><label>Temporary Redirect (307)</label></div><br>
  203. <div class="ui radio checkbox"><input type="radio" name="editStatusCode" value="301" ${statusCode == 301 ? "checked" : ""}><label>Moved Permanently (301)</label></div>
  204. </td>
  205. <td>
  206. <button onclick="saveEditRule(this);" payload="${encodeURIComponent(JSON.stringify(payload))}" class="ui small circular green icon basic button"><i class="save icon"></i></button>
  207. <button onclick="initRedirectionRuleList();" class="ui small circular icon basic button"><i class="cancel icon"></i></button>
  208. </td>
  209. `);
  210. $(".checkbox").checkbox();
  211. }
  212. function saveEditRule(obj){
  213. let payload = JSON.parse(decodeURIComponent($(obj).attr("payload")));
  214. let redirectUrl = $("#editRedirectUrl").val();
  215. let destUrl = $("#editDestUrl").val();
  216. let forwardChildpath = $("#editForwardChildpath").is(":checked");
  217. let statusCode = parseInt($("input[name='editStatusCode']:checked").val());
  218. $.cjax({
  219. url: "/api/redirect/edit",
  220. method: "POST",
  221. data: {
  222. originalRedirectUrl: payload.RedirectURL,
  223. newRedirectUrl: redirectUrl,
  224. destUrl: destUrl,
  225. forwardChildpath: forwardChildpath,
  226. redirectType: statusCode,
  227. },
  228. success: function(data){
  229. if (data.error != undefined){
  230. msgbox(data.error, false);
  231. }else{
  232. msgbox("Redirection rule updated", true);
  233. initRedirectionRuleList();
  234. }
  235. }
  236. });
  237. }
  238. function initRegexpSupportToggle(){
  239. $.get("/api/redirect/regex", function(data){
  240. //Set the checkbox initial state
  241. if (data == true){
  242. $("#redirectRegex").parent().checkbox("set checked");
  243. }else{
  244. $("#redirectRegex").parent().checkbox("set unchecked");
  245. }
  246. //Bind event to the checkbox
  247. $("#redirectRegex").on("change", function(){
  248. $.cjax({
  249. url: "/api/redirect/regex",
  250. method: "POST",
  251. data: {"enable": $(this)[0].checked},
  252. success: function(data){
  253. if (data.error != undefined){
  254. msgbox(data.error, false);
  255. }else{
  256. msgbox("Regex redirect setting updated", true);
  257. }
  258. }
  259. });
  260. });
  261. });
  262. }
  263. initRegexpSupportToggle();
  264. $("#rurl").on('change', (event) => {
  265. const value = event.target.value.trim().replace(/^(https?:\/\/)/, '');
  266. event.target.value = value;
  267. });
  268. </script>