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]');
}
}
}
Related Posts:
- redirect does not work in ajax function
- Authentication / login mechanism (non wp-admin)
- Set front page as static page [closed]
- WordPress Theme Development: How to redirect templates the right way?
- Redirect to specified page when insufficient permissions?
- Where to find the code that redirects requests on the static page URL to the home page
- I used single quote to design HTML of my WordPress blog. Now site redirects to index.php everytime than page.php or single.php
- Disable redirect canonical
- Developing WordPress site behind a static site
- What is the best practice workflow for new website overhaul and keeping current SEO ranking? [closed]
- Check if redirected from a specific page template
- How to add posts to custom menus?
- How to add custom css file in theme?
- CSS not updating in browser when I change it
- Is wp_is_mobile() effective?
- Display Menu Name using wp_nav_menu
- What questions do you ask when asked to give an estimate on theme development?
- How to remove search bar from a wordpress theme? [closed]
- Get url of thumbnail from the media uploader
- How to: Update live sites theme from version control repo via plugin or library? [closed]
- Adding items to page template dropdown on Page Edit Screen
- How to use logout function on custom menu link?
- Hide certain pages / posts on wp-admin, show custom filter
- use add_action(‘wp_head’) in a widget for generating dynamic CSS styles
- Front End Post Submit Form
- Page attribute template dropdown not displayed even the syntax is correct
- How to add a text widget during theme activation
- WordPress Theme customisation CSS
- Displaying wp menus by name without using theme locations
- Why does this loop only work on the homepage?
- Nesting Functions within Functions
- Theme check: Missing a text-domain
- Can I customize any WordPress parent block-based theme template files by child theme like a parent classic theme?
- Do I really need the div class entry?
- How to determine if post has widget content?
- Custom Script Section Only Echoes Text
- Best way to start becoming a wordpress developer
- Set multiple templates per post
- Place the page title into the short code
- WordPress is adding margins and padding to my custom menu?
- What is the best way to organize template parts?
- Can’t login unless twentyfifteen theme is active
- Keep sticky posts out of query unless they have featured image
- wp_enqueue_script outside functions.php file?
- How to implement a $_SESSION alternative in WordPress inside a theme without a plugin?
- How can I make my options in an array and store theme in WP options one DB row?
- Differences between developing custom themes for wordpress.com and wordpress.org?
- Easy to develop on a URL that is changed for production?
- How to show custom meta_key in each li of menu
- Can I add multiple arrays within add_theme_support( ‘colors’ )?
- How to control on which pages the Aldehyde theme’s main slider is shown?
- Theme Customizer not loading JS for live preview
- Replace function in a child theme
- get_search_form() and aria_label
- Can’t remove DIV from hooks in Storefront child theme [closed]
- Admin: sub menu doesnt display under apperance when activate my themes
- Get data from style.css file and from from users->your profile
- How to register and enqueue JavaScript files without breaking plugin dependencies?
- How can I specify that an area of my theme contains widgets?
- How to dequeue css files?
- Extract all shortcode data from post into loop variables?
- wp_nav_menu and its fallback
- Adding the_content() return warning count()
- Copying the theme style files and images to duplicate the website but with a difference
- How to set Post meta-box defaults based on the choices made by user in Customizer?
- Ajax call returns 0 when add_action is inside a class in functions.php
- Woocommerce Product attribute not imported with wordpress Importer [closed]
- Is it possible to have a Theme with built-in physical page files?
- header, stylesheet not being read
- first excerpt fine, subsequent post excerpts shift to the right instead of displaying vertically [closed]
- iPad WordPress theme?
- Adding link post format to theme and permalink to rss feed
- List categories of a post hierarchically?
- Custom Post Types set up, how do I style the main slug page?
- What are the permalinks options for “Category” base and removing it?
- Designing a custom archive.php inspired by the Autofocus theme
- Is a site with 1,500 pages, (1000 of which are E-Commerce Pages) Too Big to Migrate to WordPress?
- How to get blog-id of an MU site from functions.php
- How to register dynamic settings in WordPress Customizer?
- How to add audio files to audio player in WordPress custom theme?
- WordPress theme doesn’t read my translations from pt_BR.po file
- Data Validation & Sanitization for Big HTML Blocks
- How to get full native language instead of iso?
- Move the social media icons to the left of a WordPress nav menu for Soledad child Theme
- Two instances of the theme folder in the URL
- Next post in category
- Horizontal Navigation
- Custom image size doesn’t work
- Paginated WP_Query doesn’t return 404’s, even when posts don’t exist
- How to assign the default file at “Appearance > Editor”?
- Not Found when using activity stream as front page with BuddyPress
- Setting different CSS for all pages except home.php
- load src of images that attachs in wordpress’s post
- Add Field To All Pages
- Creating custom function in wordpress to return data from database
- How customizable is a self-hosted WordPress blog compared to a Blogger blog?
- WP Customizer get control value on change
- Activate Gutenberg group block for theme
- how can i modify the default wordpress sidebar
- Which is recommended to learn first: classic themes or block themes?