Guest User

Redirect domain.com/post-name to domain.com/blog/post-name

a guest
Feb 19th, 2024
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. function custom_redirect_404_to_blog() {
  2. if (is_404()) {
  3. // Get the current URI without the home URL
  4. $requested_uri = $_SERVER['REQUEST_URI'];
  5.  
  6. // Check if the URI does not already start with '/blog/' to avoid infinite redirection
  7. if (substr($requested_uri, 0, 6) !== '/blog/') {
  8. // Construct the new URI by prepending '/blog' to the requested URI
  9. $new_uri = '/blog' . $requested_uri;
  10.  
  11. // Perform the redirection to the new URI
  12. wp_redirect(home_url($new_uri), 301);
  13. exit;
  14. }
  15. }
  16. }
  17. add_action('template_redirect', 'custom_redirect_404_to_blog');
Advertisement
Add Comment
Please, Sign In to add comment