Skip to content

Instantly share code, notes, and snippets.

@overwine
Created November 26, 2013 01:28
Show Gist options
  • Save overwine/7651983 to your computer and use it in GitHub Desktop.
Save overwine/7651983 to your computer and use it in GitHub Desktop.
.htaccess making any www url go to the homepage
RewriteEngine On
# Ugh wordpress - wordpress apparently doesn't like it if you refer to the
# site interchangably between www.domain.com and domain.com, so rewrite
# the non-www version to the www version here to avoid problems. If we don't
# do this, going to domain.com/anything-at-all will redirect to the root of
# www.domain.com, which is not helpful.
# NOTE: This is a 302 redirect, not a 301, as at some point we might want to
# make the non-www site the "canonical" one
# FIX - redirets realmswork.com as well
RewriteCond %{HTTP_HOST} ^(wolflair|realmswork)\.com$ [NC]
RewriteRule ^(.+)$ http://www.realmswork.com/$1 [R=302,L,E=nocache:1]
# Downloads should be sent back to our other server until we have everything
# hosted in one place - we use the download.wolflair.com subdomain for this,
# which goes to the download folder on the old server
# NOTE: For explanation of nocache=1, see below
# FIX - redirets realmswork.com as well
RewriteCond %{HTTP_HOST} ^(www\.)?(wolflair|realmswork)\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/download/ [NC]
RewriteRule ^download/(.+)$ http://download.wolflair.com/$1 [R=302,L,E=nocache:1]
# If someone tries to visit a non-existent subdomain, send them back to the
# main web site instead
# NOTE: For explanation of nocache=1, see below
RewriteCond %{HTTP_HOST} ^(.+?)\.wolflair\.com$ [NC]
RewriteCond %{HTTP_HOST} !^(www|testing)\.wolflair\.com$ [NC]
RewriteRule ^ http://www.wolflair.com%{REQUEST_URI} [R=302,L,E=nocache:1]
# Anything trying to access the old Realm Works page should be sent to the new
# one
# FIX - redirets realmswork.com as well
RewriteCond %{HTTP_HOST} ^(www\.)?(wolflair|realmswork)\.com$ [NC]
RewriteCond %{QUERY_STRING} context=realm_works [NC]
# Drop the query string off the URL by specifying an empty one with ? - if we
# don't do this, the old context=realm_works query string is automatically
# appended
RewriteRule ^ http://www.wolflair.com/realmworks? [L,R=301,E=nocache:1]
# FIX - need similar rules for other migrated content
# Direct appropriate thingies to legacy site
# FIX - redirets realmswork.com as well
RewriteCond %{HTTP_HOST} ^(www\.)?(wolflair|realmswork)\.com$ [NC]
# Avoid redirect loops
RewriteCond %{REQUEST_URI} !^.*legacy [NC]
# Anything with an old context= parameter needs to be redirected to the legacy
# site, with the exception of the bits that have already been moved and should
# be redirected above
RewriteCond %{QUERY_STRING} context=(?!realm_works) [NC,or]
# All images under the legacy folders need to be available
RewriteCond %{REQUEST_URI} ^/(army_builder|card_vault|forums|hero_lab|more|msxml|newsletter|realm_works|retailer_info|root|store|tech_support|tournament_ace|userroot)/images/ [NC,or]
# All menu buttons for those folders must also be available
RewriteCond %{REQUEST_URI} ^/(army_builder|card_vault|forums|hero_lab|more|msxml|newsletter|realm_works|retailer_info|root|store|tech_support|tournament_ace|userroot)/menu_button.png [NC,or]
# Some important files also need to be available at the root
RewriteCond %{REQUEST_URI} ^/(root/bugreport|welcome_.+|hero_lab_mac_beta|landing_page_.+|realm_works_.+|reassign|realm_works/beta_telemetry)\.php [NC,or]
# Finally, a number of special folders must also be available at the site root
RewriteCond %{REQUEST_URI} ^/(images/|lightbox|photos|slimbox|static) [NC]
RewriteRule ^(.+)$ /legacy/$1 [L,E=nocache:1]
# Anything in the redirect folder needs to have caching disabled, otherwise
# browsers will cache it and we won't be able to change it
# FIX - redirets realmswork.com as well
RewriteCond %{HTTP_HOST} ^(www\.)?(wolflair|realmswork)\.com$ [NC]
RewriteRule ^redirect - [E=nocache:1]
# Disable caching if the "nocache" environment variable is set in the
# RewriteRules above. We do this because otherwise there's no way to expire
# a 301 or 302 redirect from the cache without the user jumping through a lot
# of hoops, and we want to retain flexibility with where we send things as much
# as possible.
Header always set Cache-Control "no-store, no-cache, must-revalidate" env=nocache
Header always set Expires "Thu, 01 Jan 1970 00:00:00 GMT" env=nocache
# NOTE - wordpress rewrites must come AFTER the above, as they should only
# affect anything that isn't handled by e.g. the legacy site rewrites.
<IfModule mod_rewrite.c>
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]
</IfModule>
# END WordPress
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment