Skip to content

Instantly share code, notes, and snippets.

@gregrickaby
Last active November 26, 2018 01:38
Show Gist options
  • Save gregrickaby/5619593 to your computer and use it in GitHub Desktop.
Save gregrickaby/5619593 to your computer and use it in GitHub Desktop.
301 Redirects with .htaccess

Below are some examples of how to deal with 301 redirects in .htaccess

Place the example code below into .htaccess

# Redirect an entire website
Redirect 301 / http://new-url.com
# Redirect WordPress local development uploads, to dev/prod server.
RedirectMatch 301 ^/wp-content/uploads/(.*) http://mylivesite.com/wp-content/uploads/$1
<?php
/**
* Force old permalinks to a new page.
*/
function wds_client_301_redirect() {
// Get the URL.
$url = untrailingslashit( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
// Redirect to the correct page!
if ( is_404() && ( false !== strpos( $url, 'old-page-url' ) ) ) {
wp_redirect( 'https://new-url.com/new-page-url', 301 );
}
}
add_action( 'template_redirect', 'wds_client_301_redirect' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment