Skip to content

Instantly share code, notes, and snippets.

@taras
Forked from mikeschinkel/redirect-to-www.php
Last active July 13, 2016 19:07
Show Gist options
  • Save taras/aadfb65c1386a162afbef43695788e4f to your computer and use it in GitHub Desktop.
Save taras/aadfb65c1386a162afbef43695788e4f to your computer and use it in GitHub Desktop.
MU-Plugin for WordPress to redirect from non-www to www domain
<?php
if ( ! preg_match( '#wp-json#', $_SERVER['REQUEST_URI'] ) && ! preg_match( '#wp-content#', $_SERVER['REQUEST_URI'] ) && ! preg_match( '#^www.#', $_SERVER['HTTP_HOST'] ) ) {
$https = 443 === $_SERVER['SERVER_PORT'] ? 's' : '';
header( "Location: http{$https}://www.{$_SERVER['HTTP_HOST']}", true, 301 );
die();
}
@mikeschinkel
Copy link

You can collapse the first line down to this:

if ( ! preg_match( '#wp-(json|content)#', $_SERVER['REQUEST_URI'] )  && ! preg_match( '#^www.#', $_SERVER['HTTP_HOST'] ) ) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment