multisite - Disable "similar permalink" redirect

admin2025-06-06  14

This question already has answers here: How to prevent automatic redirection of 404 errors and "incorrect" URLs? (3 answers) Closed 6 years ago.

I have set a post to private on a WordPress multisite so users should be redirected to a 404 page. The permalink of this post is .

Now WordPress seems to redirect requests to event to event-30-01-2016. WordPress seems to focus on the lower number after the event string. Example: it will prioritise event-29-05-2015 over event-30-05-2015.

I want to keep the URLs for posts like event-* but I want event to redirect to 404 since I kind of disabled the page.

My settings on /wp-admin/options-permalink.php are set to Post name.

I don't have any special rewriteRules in .htaccess for events.

This is my .htaccess:

# BEGIN WordPress
RewriteEngine on
RewriteBase /
RewriteRule ^index\.php$ - [L]

# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]
# END WordPress

Is there maybe a hook that I could use in order to disable this kind of redirection?

This question already has answers here: How to prevent automatic redirection of 404 errors and "incorrect" URLs? (3 answers) Closed 6 years ago.

I have set a post to private on a WordPress multisite so users should be redirected to a 404 page. The permalink of this post is http://local.website/en/event.

Now WordPress seems to redirect requests to event to event-30-01-2016. WordPress seems to focus on the lower number after the event string. Example: it will prioritise event-29-05-2015 over event-30-05-2015.

I want to keep the URLs for posts like event-* but I want event to redirect to 404 since I kind of disabled the page.

My settings on /wp-admin/options-permalink.php are set to Post name.

I don't have any special rewriteRules in .htaccess for events.

This is my .htaccess:

# BEGIN WordPress
RewriteEngine on
RewriteBase /
RewriteRule ^index\.php$ - [L]

# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]
# END WordPress

Is there maybe a hook that I could use in order to disable this kind of redirection?

Share Improve this question edited Nov 28, 2018 at 5:43 butlerblog 5,1413 gold badges28 silver badges44 bronze badges asked Nov 27, 2018 at 21:59 zyrupzyrup 1034 bronze badges 3
  • Try this wordpress.stackexchange/questions/71927/… – Nikolay Commented Nov 28, 2018 at 18:42
  • @Nikolay oh.. thank you a lot! I'll add the script in the answer that helped me. Or maybe you would like to answer the question? remove_action('template_redirect', 'redirect_canonical'); helped me – zyrup Commented Nov 28, 2018 at 20:42
  • You don’t want to completely remove canonical redirection! Check for your specific case and only remove it for those requests. – Milo Commented Nov 28, 2018 at 22:52
Add a comment  | 

1 Answer 1

Reset to default 0

To completely disable this feature, use this code, which I got from here.

remove_action( 'template_redirect', 'redirect_canonical' );

To only disable it for URL addresses that contain the string 'event', use this code:

add_filter( 'redirect_canonical', 'disable_redirect_canonical_for_event', 2, 10 );

function disable_redirect_canonical_for_event( $redirect_url, $requested_url ) {
    if ( strpos( $requested_url, 'event' ) !== false ) {
        return false;
    }
    return $redirect_url;
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749143294a316734.html

最新回复(0)