redirection.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <h3><i class="level up alternate icon"></i> Redirection Rules</h3>
  2. <p>Add exception case for redirecting any matching URLs</p>
  3. <div class="ui basic segment">
  4. <table class="ui very basic celled table">
  5. <thead>
  6. <tr>
  7. <th>Redirection URL</th>
  8. <th></th>
  9. <th>Destination URL</th>
  10. <th>Copy Pathname</th>
  11. <th>Status Code</th>
  12. <th>Remove</th>
  13. </tr>
  14. </thead>
  15. <tbody id="redirectionRuleList">
  16. <tr>
  17. <td></td>
  18. <td></td>
  19. <td></td>
  20. <td></td>
  21. </tr>
  22. </tbody>
  23. </table>
  24. <div class="ui green message" id="delRuleSucc" style="display:none;">
  25. <i class="ui green checkmark icon"></i> Redirection Rule Deleted
  26. </div>
  27. </div>
  28. <div class="ui divider"></div>
  29. <p>Add path redirection to your domain</p>
  30. <div class="ui divider"></div>
  31. <div class="ui form">
  32. <div class="field">
  33. <label>Redirection URL</label>
  34. <input type="text" id="rurl" name="redirection-url" placeholder="Redirection URL">
  35. <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>
  36. </div>
  37. <div class="field">
  38. <label>Destination URL</label>
  39. <input type="text" name="destination-url" placeholder="Destination URL">
  40. <small><i class="ui circle info icon"></i> The target URL request being redirected to, e.g. dest.example.com/mysite</small>
  41. </div>
  42. <div class="field">
  43. <div class="ui checkbox">
  44. <input type="checkbox" name="forward-childpath" tabindex="0" class="hidden" checked>
  45. <label>Forward Pathname</label>
  46. </div>
  47. <div class="ui message">
  48. <p>Append the current pathname after the redirect destination</p>
  49. <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>
  50. <i class="square outline icon"></i> Disabled old.example.com<b>/blog?post=13</b> <i class="long arrow alternate right icon" style="margin-left: 1em;"></i> new.example.com
  51. </div>
  52. </div>
  53. <div class="grouped fields">
  54. <label>Redirection Status Code</label>
  55. <div class="field">
  56. <div class="ui radio checkbox">
  57. <input type="radio" name="redirect-type" value="307" checked>
  58. <label>Temporary Redirect <br><small>Status Code: 307</small></label>
  59. </div>
  60. </div>
  61. <div class="field">
  62. <div class="ui radio checkbox">
  63. <input type="radio" name="redirect-type" value="301">
  64. <label>Moved Permanently <br><small>Status Code: 301</small></label>
  65. </div>
  66. </div>
  67. </div>
  68. <button class="ui teal button" onclick="addRules();"><i class="ui plus icon"></i> Add Redirection Rule</button>
  69. <div class="ui green message" id="ruleAddSucc" style="display:none;">
  70. <i class="ui green checkmark icon"></i> Redirection Rules Added
  71. </div>
  72. </div>
  73. <script>
  74. $(".checkbox").checkbox();
  75. function addRules(){
  76. let redirectUrl = document.querySelector('input[name="redirection-url"]').value;
  77. let destUrl = document.querySelector('input[name="destination-url"]').value;
  78. let forwardChildpath = document.querySelector('input[name="forward-childpath"]').checked;
  79. let redirectType = document.querySelector('input[name="redirect-type"]:checked').value;
  80. $.ajax({
  81. url: "/redirect/add",
  82. method: "POST",
  83. data: {
  84. redirectUrl: redirectUrl,
  85. destUrl: destUrl,
  86. forwardChildpath: forwardChildpath,
  87. redirectType: parseInt(redirectType),
  88. },
  89. success: function(data){
  90. if (data.error != undefined){
  91. alert(data.error);
  92. }else{
  93. $("#ruleAddSucc").stop().finish().slideDown("fast").delay(3000).slideUp("fast");
  94. }
  95. initRedirectionRuleList();
  96. }
  97. });
  98. }
  99. function deleteRule(obj){
  100. let targetURL = $(obj).attr("rurl");
  101. targetURL = JSON.parse(decodeURIComponent(targetURL));
  102. if (confirm("Confirm remove redirection from " + targetURL + " ?")){
  103. $.ajax({
  104. url: "/redirect/delete",
  105. method: "POST",
  106. data: {
  107. redirectUrl: targetURL,
  108. },
  109. success: function(data){
  110. if (data.error != undefined){
  111. alert(data.error);
  112. }else{
  113. $("#delRuleSucc").stop().finish().slideDown("fast").delay(3000).slideUp("fast");
  114. }
  115. initRedirectionRuleList();
  116. }
  117. });
  118. }
  119. }
  120. function initRedirectionRuleList(){
  121. $("#redirectionRuleList").html("");
  122. $.get("/redirect/list", function(data){
  123. data.forEach(function(entry){
  124. $("#redirectionRuleList").append(`<tr>
  125. <td>${entry.RedirectURL} </td>
  126. <td style="text-align: center;"><i class="blue arrow right icon"></i></td>
  127. <td>${entry.TargetURL}</td>
  128. <td>${entry.ForwardChildpath?"<i class='ui green checkmark icon'></i>":"<i class='ui red remove icon'></i>"}</td>
  129. <td>${entry.StatusCode==307?"Temporary Redirect (307)":"Moved Permanently (301)"}</td>
  130. <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>
  131. </tr>`);
  132. });
  133. if (data.length == 0){
  134. $("#redirectionRuleList").append(`<tr colspan="4"><td><i class="checkmark icon"></i> No redirection rule</td></tr>`);
  135. }
  136. });
  137. }
  138. initRedirectionRuleList();
  139. $("#rurl").on('change', (event) => {
  140. const value = event.target.value.trim().replace(/^(https?:\/\/)/, '');
  141. event.target.value = value;
  142. });
  143. </script>