0

Should be a fairly simple procedure. I've done a fair share of internet searching and can't get the htaccess rewrite to work in /public_html

Using a VPS Apache server, any idea's why? Thank you in advance!

Trying both the older RedirectMatch 301 and the mod_rewrite RewriteRuleas (one at a time) as below:

# New Post URLs, without dates, requires redirects for old links.
RedirectMatch 301 ^/([0-9]+)/([0-9]+)/(?!page/)(.+)$ https://www.url.org/$3
# or RedirectMatch 301 ^/([0-9]{4})/([0-9]{2})/(?!page/)(.+)$ https://www.url.org/$3

 BEGIN WordPress
 <IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteBase /
 RewriteRule ^([0-9]{4})/([0-9]{2})/([a-z0-9\-/]+)$ /$3 [R=301,NC,L]
etc...
  • Please include examples of the URLs you want to redirect from/to. – MrWhite Apr 25 '18 at 23:12
2

You can actually change them in Settings -> Permalinks and it will change the link that generated for the post. And if you still be unhappy with result you can remove WordPress rewrite rules for dates with date_rewrite_rules filter.

UPDATE:

In this case, you can use post_rewrite_rules to keep the date to post rule working and implement redirect during tempalte_redirect action-hook. You can plug this code into your codebase and flush rewrite rules in WordPress. Should work right after that.

add_filter( 'post_rewrite_rules', function( $rules ) {
    global $wp_rewrite;
    $rule = [ '([0-9]{4})/([0-9]{1,2})/([^/]+)(?:/([0-9]+))?/?$' =>
        'index.php?year=$matches[1]&monthnum=$matches[2]&name=$matches[3]' ];
    return array_merge($rules, $rule);
});

add_filter('template_redirect', function(){
    global $wp_query;

    if ( ! is_single() ) {
        return;
    }

    if ( $wp_query->get('monthnum') !== "" && $wp_query->get('year') !== "" ) {
        wp_redirect( get_permalink(), 301 );
    }
});
|improve this answer|||||
  • Settings->Permalinks is already being done but we have to address the posts already created. The .htaccess URL rewrite is for all the urls already in circulation for existing posts so users don't get a 404 error! When you say date_rewrite_rules are you talking about hooks located in the WP_Rewrite class in /wp-includes/rewrite.php – qwueryIT Apr 25 '18 at 19:30
  • Does the following apply to existing URLs in circulation? $date_rewrite (array) The rewrite rules for date archives. File: wp-includes/class-wp-rewrite.php – qwueryIT Apr 25 '18 at 19:40
  • check updated answer – Oleg Butuzov Apr 25 '18 at 22:08
  • Thank you for the code! Appreciate the help. So is this supposed to just go anywhere in /wp-includes/class-wp-rewrite.php? It doesn't seem to work. Do I need to do something special to hit the tempalte_redirect action-hook? Do I need to put some code in /wp-includes/template-loader.php – qwueryIT Apr 27 '18 at 1:10
  • Don't change the wp course code, do your injects via plugin/there wherever it makes more sense. Do some google on how to add your code to functions.php or how to create a simple plugin. – Oleg Butuzov Apr 27 '18 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.