skip to Main Content

I’d like to know how to do this using header.php (WordPress) or any other method. Basically, I want to set up a redirect from a subdirectory page (e.g. "/my-ebook/") to an external website (e.g. Gumroad).

So, when people visit "/my-ebook/", they’re redirected to my Gumroad profile page, for example. I know a plugin can do this, but want to know a way to do this without using a plugin. Thank you in advance.

2

Answers


  1. The template_redirect action is probably the most appropriate action to use, but you could probably use an mu-plugin to run as early as possible.

    Here’s with the template_redirect action (add to your theme’s functions.php):

    add_action( 'template_redirect', static function () {
        if ( empty( $_SERVER['REQUEST_URI'] ) || ! str_contains( '/my-ebook/', $_SERVER['REQUEST_URI'] ) ) {
            return;
        }
        
        nocache_headers();
        
        wp_redirect( ... );
    
        exit;
    } );
    

    For an mu-plugin, take out the code within the action’s callback and place it in wp-content/mu-plugins/gumroad-redirect.php:

    <?php
    
    if ( empty( $_SERVER['REQUEST_URI'] ) || ! str_contains( '/my-ebook/', $_SERVER['REQUEST_URI'] ) ) {
        return;
    }
    
    nocache_headers();
    
    wp_redirect( ... );
    
    exit;
    
    Login or Signup to reply.
  2. You can use multiple method for it but here is suggested 2 methods

    first for functions.php

    
    function redirect_my_ebook_page() {
        if (is_page('my-ebook')) { // Check if it's the "my-ebook" page
            wp_redirect('https://gumroad.com/your-profile'); // Redirect to Gumroad
            exit(); // Always call exit after wp_redirect
        }
    }
    add_action('template_redirect', 'redirect_my_ebook_page');
    
    

    And second method is .htaccess

    Redirect 301 /my-ebook/ https://gumroad.com/your-profile
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search