Cheatsheet htaccess Codes for WordPress and Beyond

September 1, 2017
htaccess, Solutions, Wordpress

While the items below can sometimes be done within plugins, adding them to the .htaccess file is more secure and often easier. If you already know what you’re looking for, the cheatsheet is below. Otherwise, let’s explain briefly what a .htaccess file is.

What is .htaccess?

The .htaccess file is a file that controls users when they first enter your site. It looks like a simple text file, but instead of a filename, it just has an ending: “.htaccess” . Note the period at the start. This is a hidden file, so you might not always see it, and it’s not always included in sites either.

But despite it’s simple setup, .htaccess is a powerful file. It can forward, redirect, pull images from another site, set up maintenance pages, setup the caching rules, and even is used by WordPress to make permalinks (pretty links). You should be careful when dealing with .htaccess though. If you write something wrong in your file, your entire site could stop working immediately. It’s best to leave edits to this file to programmers who know what they’re doing!

The Cheats – Copy and Paste

The initial two below are primarily used because without them, you would end up with multiple versions of the your site – not a good thing for your SEO.

Redirect Http to https

<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteRule ^(.*)$$1 [R=301]

Redirect www to non-www

Redirect WWW to non-WWW
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$$1 [R=301,L]

It’s important to do a 301 redirect on any pages or folders that are now in different areas on a new site. Google will then understand that these are the new pages though the URL has changed.

Simple 301 Redirects

RedirectMatch 301 ^/top-kid-birthday-party$ /las-top-kids

Parent Pages (WordPress) 301 Redirects

RedirectMatch 301 ^/about-us/?$ /about-us/who-we-are

301 Redirect an entire folder

RedirectMatch 301 /wfc-content/uploads/(.*) /bcc-content/uploads/$1

If you’re working with a WordPress site on your localhost, but don’t want to sync your images, you can use the coding below on your localhost site to pull the images from the live site. Fantastic!

Pull Remote Images (live site) into a localhost site

<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteCond %{REQUEST_FILENAME} !-f [NC]
 RewriteRule ^(.*\.(js|css|png|jpe?g|gif|ico))$1 [NC,P,L]

OR, if you’ve also renamed the wp-content folder, you can use the coding below:

<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteBase /
 RewriteCond %{REQUEST_FILENAME} !-f [NC]
 RewriteRule ^K12345p/uploads/(.*)$$1 [L,R=301,NC]

Want to set up a maintenance page when you’re working on the website? This coding will force everyone except the IP address in the 3rd line (so you can view the site while you work on it) to go to the maintenance page.

Maintenance Page Redirect

<IfModule mod_rewrite.c>
 RewriteEngine on
 RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000
 RewriteCond %{REQUEST_URI} !/maintenance.html$ [NC]
 RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
 RewriteRule .* /maintenance.html [R=302,L]

Is your IP address IPv6? Use this instead for the IP address line:

RewriteCond %{REMOTE_ADDR} !^[1234:e000:1234:7c:1a23:c2ff:feda:1f12]

Looking for the default WordPress htaccess code for permalinks? Here it is! All other .htaccess coding you add should be added ABOVE this coding so the user hits it first.

Default WordPress permalinks

# BEGIN WordPress
 <IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteBase /
 RewriteRule ^index\.php$ - [L]
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule . /index.php [L]
 # END WordPress

Join the Discussion

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>