Redirect Old .php URLs to New WordPress Page

I have an old website that I’m revamping with a custom new WordPress theme. The old site had a custom (non-WP based) downloads archive that mapped URLs like this:

/downloads/view.php?id=2

Read More

The ID maps to a download item in a separate MySQL database.

I’d like to move everything over to WordPress in this refresh and I’m going to be using a custom post type to handle the downloads section, with a custom field that maps the “old ID” to the new post. This isn’t difficult and I know how to do this, but the issue becomes remapping the old URLs to the new locations.

The issue I see is that the older URLs are all PHP files (have an extension of .php). How would I go about create a plugin or WordPress function in my theme’s functions.php file to redirect these old URLs to a new URL. Essentially, I just need to have a WordPress function redirect from /downloads/view.php?id=2 to /downloads/2/, or another more optimized URL (/downloads/title-of-archive).

How would I go about handling pages with a URL of “/downloads/view.php” and “/downloads/download.php” and performing the redirect?

I know I can use the .htaccess file, but I’d prefer to do this directly in a WordPress plugin if possible for portability…

Related posts

Leave a Reply

4 comments

  1. I ended up solving the issue by hooking onto the “template_redirect” action. After that, I check if the page is_404() and then check if the URL matches my pattern. I set the appropriate header (301 versus the 404 that normally would be triggered) and perform my redirect. My code is below.

    add_action('template_redirect', 'handle_download_urls');
    
    function handle_download_urls(){
        if(is_404()){
            if(preg_match('//downloads/(?:view.php|download.php)/', $_SERVER['REQUEST_URI']) && isset($_GET['id'])){
                // Do lookup of post based on custom metadata field
                // Redirect and change header
                status_header(301);
                header('Location: [URL]');
           }
        }
    }
    
  2. You could do this with rewrites, but honestly, it’s not a rewrite what you want. A rewrite would just have the old url to act like the new url instead of redirecting it to the new location. Please not that you still need to make sure /downloads/id/ is handled by your WordPress. Now there you could use rewrite.

    Add the following code to your templates function.php:

    if ( !function_exists( 'get_current_url' ) ) {
        /**
         * Gets the current displayed url
         * @returns the url
         */
        function get_current_url( ) {
            $url  = isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] == 'on' ? 'https://' : 'http://';
            $url .= ( strlen( $_SERVER['SERVER_NAME'] ) ? $_SERVER['SERVER_NAME'] : $_SERVER['HTTP_HOST'] );
            $url .= ( strval( $_SERVER['SERVER_PORT'] ) != '80' ) ? $_SERVER['SERVER_PORT'] : '';
            $url .= $_SERVER[ 'REQUEST_URI' ];
            return $url;    
        }
    }
    
    /**
     * Permanently redirects old /downloads/view.php?id=X and /downloads/download.php?id=X
     * to /downloads/X/. 
     */
    function permanent_301_redirect_downloads() {  
    
        $matches = array();
        $url = get_current_url();
    
        // $matches[1] has view or download, and $matches[2] the id
        if ( preg_match( '/downloads/(view|download).php?id=([0-9]+)/?$/', $url, $matches ) ) :
            // Fetch the title of the archive. Are these posts? I wouldn't know
            $archive = $matches[2];
            wp_redirect( trailingslashit( home_url('') . '/downloads/' . $archive ) , 301 );  
            exit;
        endif;
    
    }  
    add_action( 'template_redirect', 'permanent_301_redirect_downloads' );  
    
  3. To redirect /downloads/view.php?id={some-name} to /downloads/{some-name}/, add the following to your .htaccess file, before the default WordPress rules

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^downloads/view.php?id=(.+) downloads/$1 [R=301,L]
    </IfModule>
    

    It will also let search engines know that your downloads have moved, and to update their index.

  4. Old Custom Php URLS Redirect to Home or other pages .htaccess code

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^clients.php https://mybtechnology.com/ [R=301,L]
    </IfModule>
    
    This will work 100%