I think can do:
add_action('template_redirect', 'my_func');
function my_func(){
if(is_single()){
// catch the last string
preg_match('/(.*)\/(.*?)\//', $_SERVER['REQUEST_URI'], $new_array);
$last_phrase= $new_array[2];
if(is_numeric($last_phrase)){
$id = $last_phrase;
header('location: '. get_permalink($id) , true, 301); exit;
}
}
}
Related Posts:
- Rewrite-Rules not working on a vhost, everything goes to index.php
- Custom Permalink
- corrupted URLs – can’t leave starting page
- remove SLASH on single_post but KEEP on categories and parent pages
- WordPress permalink still not working (error or not understood?)
- Need help with add_rewrite_rule
- Custom post type permalink endpoint
- Removing the redirect after changing a page’s slug
- permalink independent on the post’s name stored in the database
- URL rewrite based on a custom field value
- How do I turn off 301 redirecting posts (not canonical)?
- Subdomains to pages
- Redirect old permalinks to new permalinks
- How can I include a query string with get_permalink
- Using two permalinks for one post
- How to change URL structure for pagination pages?
- Rewrite rule never matching
- WordPress 3.4 Permalinks Redirection Problem
- Random Alphanumeric Key URLs
- Using WP rather than .htaccess to redirect pages/posts
- Date archive permalink modification
- Change permalink for a single post entry
- Does WordPress send a 301 header message when you change permalink structures?
- custom permalink/shortlink with base62 encoded post ID
- WordPress redirection problem after moving to a new server?
- query_vars in plugin not working?
- I need a custom permalink for my website
- Why is add_rewrite_endpoint incompatible with /%category%/%postname%/ permalink structure?
- Need Help Correct Regular Expression Redirect Code
- Infinite redirects after changing the permalink of a page
- Rewrite Rule for default post type
- Remove custom taxonomy base from URL
- Custom query var rewriting with only variable
- Date Archives’ Permalinks under Category folder
- add_rewrite_rule not working for me
- Using WP Rewrite, but just not “getting it”
- List / Get post redirects
- Redirect /%year%/%monthnum%/%day%/%postname% [closed]
- Rewrite CPT permalinks and include WPML language
- Custom permalink for specific category
- Rewrite rule intended for CPTs gives 404 for pages
- WordPress add_rewrite_tag working for Posts, but not working for Pages
- Redirecting all posts after changing URL structure with htaccess
- Redirect all posts permalinks from “.html” to “/”
- Need To Add Custom rewrite base url for individual user
- Permalink structure between 2 custom post types and a taxonomy
- how to redirect old blog post urls when permalinks are changed
- 301 Redirect for Non-Date Based URL’s
- Permalinks only for posts
- rewrite rule to redirect to the most recent date permalink
- How do I make 301 redirection from `/%post_id%/` to `/%postname%/`?
- Custom rewrite rule ignored
- Permalinks settings
- Resolve 404 on posts without category
- Permalink for PDF of article
- Problems with rewrite rule
- Comment author url link is wrong? How to change from authors/ to author/
- Redirecting “wrong” but working links to their canonical URL in order to prevent search engine penalties (e.g. /123/ at the end of URLs)
- add_rewrite_rule – Page Slug from “/foo-bar/” to “/foo/bar/”
- Add custom query vars to pretty permalinks
- Getting an ID from a URL, why can WP do this itself, but none of of the functions provided get it right?
- Custom permalink structure with %postname% in front of domain name
- How can you create a Permalink structure on a WordPress site?
- Redirect after permalink changes
- How can I set up a secondary permalink structure?
- Blogger To WordPress Permalink Redirection
- How to Use metada Value in Url and is it possible? [duplicate]
- Permalinks and page store
- Safest way to create a custom permalink
- rewrite based slug standar post wp
- How can i fix my permalinks?
- Adding custom query var to a single post without redirecting to its root slug
- Fire a code when permalinks updated
- .htaccess to redirect to a wp page
- How to redirect old permalinks to ones generated by [WP-Hashed-ID] plugin?
- why wordpress keeps redirecting to old page after deleting it?
- Flush rewrite rules with scheduled event (cron)
- WordPress Multisite ABSPATH and get_home_path() to check for htaccess or web.config file
- Redirect page to homepage, keeping URL
- Add slug to default post permalinks ONLY
- Disable “similar permalink” redirect [duplicate]
- Permalink structure /page/page/cpt-post gives 404 with pagination
- Custom Permlinks work, regular page permalinks don’t
- How to change the URL using rewrite rule in wordpress
- What permastrusture tags are generated out of the box right after creating custom post types and taxonomies?
- Rewrite posts url as they’re all children of a page
- a way to support totally different url structure
- Multiple Permalink Patterns for one page
- Pretty Filter URL
- Added incorrect rewrite rule now getting an error
- How to hide slug numbers with htaccess
- Add Custom Pagination Permalink
- Prevent guessing slug
- Why doesn’t WP update my .htaccess file?
- ReWrite Permalink to New Permalink Nginx
- Redirect Pretty Permalinks to Ugly Permalinks
- Pagination is not working, how to trace it properly?
- My wordpress site must be being affected by outside sources [duplicate]
- 404 posts error after 301 redirection of changed category / tag base
- Standard Htaccess directives disappear by themselves