Every site we build on the Rackspace Cloud ends up needing a lot of similar rules in the htaccess file. On my local PC I’ve collected them in a text file, that I copy paste sections from almost every day.

Most of the below examples will work on your average Linux-based server with Apache and mod_rewrite enabled. A few are RS Cloud Sites specific (as noted). In case you’ve never messed with this stuff before, note that this isn’t a single file to copy in its entirety. Each section has a different purpose.

Your average site that is engaged in an SEO campaign will likely need to redirect the non-www to www (or vice versa), rewrite multiple index pages to a single URL, standardized trailing slashes or no trailing slashes, and redirect old pages. If you are on Rackspace Cloud, increasing the file upload size and turning on error reporting can be done via the htaccess below (setting that sort of PHP INI value here is not normal for most other servers).

Options +FollowSymLinks
RewriteEngine on

# SUPPORT FOR FILE TYPES
# ---------------
AddType text/x-vcard .vcf

# INCREASE UPLOAD SIZE (RS Cloud)
# ---------------
php_value post_max_size '10M'
php_value upload_max_filesize '10M'
php_value max_execution_time 500
php_value max_input_time 500

#TURN ON PHP ERRORS (RS Cloud)
# ---------------
php_flag display_errors 1
php_value error_reporting 1 

#NON WWW - Canonical
#Not needed for WordPress or other CMS's that already redirect
# ---------------
RewriteCond %{HTTP_HOST} ^THEDOMAIN.com$ [NC]
RewriteRule (.*) http://www.THEDOMAIN.com/$1 [R=301,L]

#ALTERNATE DOMAINS (example including RS Cloud testing link)
# ---------------
RewriteCond %{HTTP_HOST} ^www.THEDOMAIN.com.php99-9.dfw1-1.websitetestlink.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^THEDOMAIN.com.php99-9.dfw1-1.websitetestlink.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.SOMEOTHERDOMAIN.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^SOMEOTHERDOMAIN.com$ [NC]
RewriteRule (.*) http://www.THEDOMAIN.com/$1 [R=301,L]

# Rewrite Any index.php or index.html into root folder
# ---------------
RewriteCond %{THE_REQUEST} /index.php HTTP [NC]
RewriteRule (.*)index.php$ /$1 [R=301,L]
RewriteCond %{THE_REQUEST} /index.html HTTP [NC]
RewriteRule (.*)index.html$ /$1 [R=301,L]

# Rewrite any non-trailing slashess into trailing slashes.
# First condition makes sure it does not match files
# ---------------
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.THEDOMAIN.com/$1/ [R=301,L]

# 301 REDIRECT OLD PAGES (without query string)
# ---------------
RewriteRule ^yourpage.html$ /legal-statement/ [NC,R=301,L]

# 301 REDIRECT OLD PAGES (with query string)
# ---------------
RewriteCond %{QUERY_STRING} ^category=Example&something=1$
RewriteRule ^blah.php$ /your-new-page/? [NC,R=301,L]

# REDIRECT ONE FILE EXTENSION TO ANOTHER
# ---------------
RewriteRule ^(.*).html$ $1.php [R=301,L]

# TEMP BLANK PAGE - Send whole site to blank page
# For taking down sites temporarily. Note it is commented out with #
#RewriteCond %{REQUEST_URI} !/blank.html$ 
#RewriteRule $ /blank.html [R=302,L]

Leave a Reply

You can use the <pre> tag to post a block of code, or <code> to highlight code within text.