• Anfrageformular
  • Erstgespräch
  • Beispiele
  • Testimonials
  • Wartungsservice
  • Redaktionsservice
  • Verlagswebsites
  • Dokumente

Bubsites

  • Angebot
  • Über uns
  • Details
  • Kontakt
  • Search
  • Angebot
  • Über uns
  • Details
  • Kontakt
  • Search

Redirects: Notes

WordPress Redirects via PHP

Create URL Aliases

add_action( 'wp', 'bubdev_redirect_login_aliases', 0 );
/**
 * Add Admin/Login URL Aliases
 *
 * Redirects to admin/login with alias URIs for all WP installations, since WP's
 * default shortcuts ('/admin' etc.) wont work with many subfolder installs.
 *
 * NOTE: DEACTIVATE IF YOUR'RE HIDING THE LOGIN PAGE!!
 */
function bubdev_redirect_login_aliases() {

  if ( is_plugin_active( '/all-in-one-wp-security-and-firewall/wp-security.php' ) ) return;

  $request_uri = trim( $_SERVER['REQUEST_URI'], '/' );
  $uri_aliases = array( 'login', 'admin', 'dashboard', 'anmeldung' );

  if ( in_array( $request_uri, $uri_aliases ) ) {
    wp_redirect( get_admin_url() );
    exit;
  }
}

Redirect Full URI

add_action( 'template_redirect', 'bubdev_redirect_shop_continue_shopping' );
/**
 * Redirect: 'Einkauf fortsetzen'
 */
function bubdev_redirect_shop_continue_shopping() {

  $request_uri = $_SERVER['REQUEST_URI'];

  if ( $request_uri === '/einkauf-fortsetzen'  ) {
    wp_redirect( home_url(), 301 );
    exit;
  }
}

Redirect Frontpage to Somewhere Else

add_action( 'template_redirect', 'bubdev_redirect_frontpage' );
/**
 * Frontpage Redirect
 */
function bubdev_redirect_frontpage() {

  if ( is_front_page() ) {
    wp_redirect( get_post_type_archive_link( 'bub_title' ), 307 );
    exit;
  }
}

Redirect Custom Post Type Archive to Frontpage

add_action( 'wp', 'bubdev_redirect_bub_blog_to_frontpage' );
/**
 * Redirect Custom Post Type Archive to Frontpage
 */
function bubdev_redirect_bub_blog_to_frontpage() {

 if ( ! is_admin() && is_post_type_archive( 'bub_post' ) ) {
  wp_redirect( home_url(), 301 ); 
  exit;
  }
}

Redirect Post Type

add_action( 'wp', 'bubdev_redirect_devposts', 0 );
/**
 * Redirect Post Type
 *
 * Control 'bub_devpost' post type accessibility.
 */
function bubdev_redirect_devposts() {

  global $post;

  if ( current_user_can( 'edit_pages' ) ) return;
  
  if ( $post->post_type === 'bub_devpost' 
      || is_post_type_archive( 'bub_devpost' )
      || is_tax( 'bub_devpost_cat' )
      || is_tax( 'bub_devpost_group' ) 
      || is_tax( 'bub_devpost_tag' ) 
     ) {
  
    wp_redirect( get_home_url() );
    exit;
  }
}

Redirect All 404s to Custom Page

add_action( 'wp', 'bubdev_redirect_all_404s_to_custom_page', 1 );
/**
 * Redirect All 404s to Custom Page (Slug: '404-nichts')
 *
 * Allows to set up a 404-not-found page within WP admin. Just create a page
 * with the slug '404-nichts' and tailor it to your liking.
 *
 * Note: 'wp_redirect' expects status code 301 or 302. Many times 404 wont work.
 * After all, a 404 has already been passed when the redirect function takes
 * over.
 *
 * Note: In case of issues (e.g. an infinite loop): Check for "Open Graph Tags"
 * being provided by more than one plugin (Yoast SEO, Nextscripts Auto Poster,
 * Jetpack ...). It can cause the redirect to crash.
 */
function bubdev_redirect_all_404s_to_custom_page() {

 if ( is_404() ) {
  wp_redirect( home_url() . '/404-nichts/', 301 ); 
  exit;
  }
}

Redirect Single Post To Blog Index

add_action( 'template_redirect', 'bubdev_redirect_post_format_aside_single_to_blog_index' );
/**
 * Redirect Single Post To Blog Index
 * 
 * Redirects all single posts to the blog page if the post format is 'aside'.
 * If unsure, check 'Settings > Reading' in WP admin.
 */
function bubdev_redirect_post_format_aside_single_to_blog_index() {

  if ( is_single() && has_post_format('aside') ) {
    $blog_index_page = get_permalink( get_option( 'page_for_posts' ) );
    wp_redirect( $blog_index_page, 301 );
    exit;
  }
}

Redirect on Logout

add_action('wp_logout','bubdev_redirect_to_home');
/** 
 * Redirect to Frontpage on Logout
 */
function bubdev_redirect_to_home() {
 wp_redirect( home_url() );
 exit();
}

Redirect Old URLs

add_action( 'template_redirect', 'bubdev_redirect_old_urls' );
/**
 * Redirect Old URLs
 *
 * Use htaccess redirect rule if an url segment can be replaced.
 */
function bubdev_redirect_old_urls() {

  // Get request uri.
  $request_uri = $_SERVER['REQUEST_URI'];

  // Get titles.
  $titles = get_posts( array(
    'post_type' => 'bub_title',
    'numberposts' => 200,
    'no_found_rows' => true,
  ));

  // Get parties.
  $parties = get_posts( array(
    'post_type' => 'bub_party',
    'numberposts' => 200,
    'no_found_rows' => true,
  ));

  // Redirect old title singles.
  foreach ( $titles as $title ) {

    $request_uri_name = str_replace( array( '/', '.html' ), '', $request_uri );
    $title_name = $title->post_name;

    if ( $request_uri_name == $title_name ) {
      wp_redirect( home_url() . '/titel/' . $title_name, 301 );
      exit;
    }
  }

  // Redirect old party singles.
  foreach ( $parties as $party ) {

    $request_uri_name = str_replace( array( '/', '.html' ), '', $request_uri );
    $party_name = $party->post_name;

    if ( $request_uri_name == $party_name ) {
      wp_redirect( home_url() . '/beteiligte/' . $party_name, 301 );
      exit;
    }
  }
}

Dashboard Redirect

add_action( 'load-index.php', 'bubdev_set_dashboard_redirect' );
/**
 * Dashboard Redirect
 *
 * Skip the dashboard after login and redirect somewhere else.
 */
function bubdev_set_dashboard_redirect() {

  wp_redirect( home_url() . '/wiki/' );
}

Redirect to Maintenance Mode

add_action( 'get_header', 'bubdev_maintenance_mode' );
/**
 * WordPress Maintenance Mode
 */
function bubdev_maintenance_mode() {

  if ( ! current_user_can( 'edit_posts' ) || ! is_user_logged_in() ) {

    wp_die( '<h1>Wartungsarbeiten</h1><br>
      Bitte versuchen Sie es etwas später wieder.');
  }
}

Redirect Attachment Pages

add_action( 'template_redirect', 'bubdev_redirect_attachment_page' );
/**
 * Redirect Attachment Pages
 *
 * Redirects attachment page to parent post.
 */
function bubdev_redirect_attachment_page() {

  if ( is_attachment() ) {

    global $post;

    if ( $post && $post->post_parent ) {
      wp_redirect( esc_url( get_permalink( $post->post_parent ) ), 301 );
      exit;
    } else {
      wp_redirect( esc_url( home_url( '/' ) ), 301 );
      exit;
    }
  }
}

Genesis Framework PHP Redirects

Redirect No-Search-Results

//* Redirect from "No search results" function to a page
remove_action( 'genesis_loop_else', 'genesis_do_noposts' );
add_action( 'genesis_loop_else', 'bub_search_not_found_redirect');
function bub_search_not_found_redirect() {

  ?>
    <script>
    var currentSite = window.location.hostname;
    var path = "/404-nichts-gefunden/";
    window.location.href = "http://" + currentSite + path;
    </script> 
  <?php

}

Redirects via .htaccess

See: https://www.bubsites.com/blog/htaccess-notes/

Redirects via URL

Example:

http://www.example.com/login/?redirect_to=http%3A%2F%2Fwww.example.com%2Fwp-admin%2F

Rubrik: Developer Notes

Stefan Buchberger

Über den Autor

Stefan Buchberger war lange als Buchverleger und Redakteur tätig, bis er seine Begeisterung für die Webtechnologie entdeckte.

Ich bin Ihr Ansprechpartner für Webentwicklung, WordPress-Wartung und weitere Services.

Seitenleiste

Top-Beiträge

Buch & Berger ist nun Bubsites Website Service

Seit August 2023 ist die Buch & Berger Webagentur als Bubsites Website Service unter www.bubsites.com zu finden. Der neue Name, eine Kurzversion …

Website-Lösungen: Maßgeschneidert oder Vorlage

Bei der Entwicklung von Websites unterscheiden wir zwischen zwei grundsätzlichen Herangehensweisen: Jede Methode hat ihre Vor- und Nachteile, die wir hier erläutern …

Projektablauf: Vom Erstgespräch zur fertigen Website

Wir entwickeln Websites für verschiedenste Einsatzbereiche, meist auf WordPress-Basis. Hier erläutern wir unsere Vorgehensweise. Angebot und Kostenschätzung Im Erstgespräch klären wir Ihre …

Workshop für Webprojekte

Ein einfaches Beratungsgespräch kann die Vielschichtigkeit eines Webprojekts oftmals nicht umfassend abbilden. Der Workshop ist daher das optimale Format, um in die …

WordPress-Wartung und technischer Service

Wir übernehmen die zuverlässige Wartung und technische Betreuung Ihrer WordPress-Website. Für direkte Anfragen besuchen Sie bitte unsere Kontaktseite. Kernpunkte der Wartungsvereinbarung Unser …

Details zur Website-Wartung und Wartungsvereinbarung

Hier erhalten Sie eine ausführliche Beschreibung unserer Wartungsvereinbarung. Für einen kurzen Überblick empfehlen wir den Beitrag: WordPress-Wartung und technischer Service. Laufende oder …

FAQ zur Wartungsvereinbarung

Sie sind bereits Wartungskunde oder interessieren sich für dieses Thema? Hier erläutern wir einige Begriffe rund um unsere Wartungsvereinbarung und Wartungsberichte. Lizenzpauschale: …

Redaktioneller Service für Ihre Website

Der Inhalt bildet das Fundament einer erfolgreichen Website. Wir sorgen dafür, dass Ihre Inhalte professionell, präzise und zielgruppengerecht sind. Unsere Web-Content-Services Warum …

Testimonials: Das sagen unsere Kunden

Ihre Zufriedenheit steht bei uns an erster Stelle. Ehrliches Feedback ist uns wichtig, um unsere Leistungen kontinuierlich zu verbessern und Ihren Anforderungen …

Website-Briefing: Ein Leitfaden zur Projektvorbereitung

Damit Ihr Webprojekt reibungslos startet, haben wir diesen Leitfaden erstellt. Er gibt uns einen klaren Überblick über Ihre Wünsche und Anforderungen und …
↑
  • Impressum
  • Datenschutz
↑

Zeige alle ...

Click to Copy