.htaccess and Other Stuff You Don’t Know About (But Should)

I love building stuff.  It’s sexy. It’s magic.  But, there are parts of the building process that ARE NOT sexy, yet very important.  One of those non-sexy, yet important parts of web design is the .htaccess file.  This post is about that.

Most of the time I do stuff that’s easy.  It’s not a confession. I’m pretty sure everyone doesn’t mind doing easy stuff, by definition. Building a brand new site is easy. An Idea + Bluehost + SimpleScript WordPress install + A theme + a few plugins = Awesome fun and Magic.  Updating an old, existing site is not as easy.  There are old file structures and existing content and established inbound links and DNS and … the little not sexy, yet important and magical .htaccess file.

The Situation

Ben and I convinced friend-of-the-podcast, Joey Boats to update his website.  We were going to  update to WordPress to allow for blogging, easy content updating and all the flexibility and goodness that WordPress allows. The old site was created with Dreamweaver templates and had been in place for a few years.  During that time, we added a new domain (an ‘addon’ domain) and had consolidated two domains under a primary domain.  The primary domain is www.pleasureislandvacationguide.com/ and the two addon domains are www.wrightsvillebeachscenictours.com  and capefearnaturalist.com  (more on this later).

Once we did all of our fun and magically sexy UX consultation and information architecture diagrams and flows and established goals and SMART objectives… well, I was tired and pretty proud of myself.  I KNEW that the old links would break, but I figured that a quick trip to the … what’s the name of that small file… is it robot.txt? no.  haccess.txt? htaccess file? [DOT]htaccess file!

The .htacess file

Looking back, the .htaccess file is pretty easy.  But, as someone new to working with WordPress, the .htacess file gave me a fit for two days.  I’ve come to respect it.  It’s not sexy.  It’s a bitch.   But, it’s the only way to fix all the broken links our site renovation created. So, I had to hold my breath and dive in.  Below is my report.

Step one – Edit the .htaccess file

This would seem to be an easy thing.  I’ve done it before.  Search for 301 redirect and copy / paste. Make a few edits to reflect your new and old domain / file structure. Then go on break, you web building magician.  Was not to be… This is what I expected to work:

redirect 301 /old/old.htm http://www.newdomain.com/somefile.html
(from a simple search to jesperastrom.com)

This is what I saw instead:

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

# END WordPress

 

This is not what I expected.  It doesn’t even say redirect.  I tried to paste the copied code below… and above… with spaces and without… Nothing worked.  No worries, I thought.  “WordPress is doing something with the .htaccess file”.  Keep Calm and Google On.

Step two – research 301 redirects

As we say, “Google’s your friend”.  I search for .htaccess files and WordPress and 301 redirects and RewriteRule.  The stuff I got back was either irrelevant or so dang technical.  I’m a magician.  I make big things happen.  Now I have to read and understand something new.  That would take hours… oh!  I find a plug-in.  God Bless you, WordPress community.

Step Three – Simple 301 redirect WordPress plugin

The Simple 301 Redirects wordpress plugin seemed to be the answer.  It worked within the WordPress system.  I wish I could report that I grok’ed the internal workings of it, but it remains a mystery and the plugin site doesn’t give much insight.  I imagine it has something to do with the permalink system… because the actual .htaccess file is unchanged.

Step 3.5 or Problem 2 – Capenaturalist.com(CFN) and WrightsvilleBeachScenicTours.com(WBST)

The simple 301 Redirects are working… but only on one domain – WBST.  CFN requests return a 404 page… in the proper WBST format.  Huh.  This is when it’s good to have a coding / building buddy.  I explained it to Ben and he -correctly- figured the problem was with all the domain forwards and addon.  “Map all those domains out first and see what you get”, was he advice.

Step Four – Webmaster Tools

I noticed that WBST and CFN links were showing up in the search results.  That’s was the last straw and signed the death warrant on CFN.  I wanted all references to CFN to stop.  It’s confusing and it’s breaking stuff.  Time to die, CFN.

Webmaster Tools is a great place to see the site errors.  I was in there to check on 404 errors and get a list of files to redirect with the Simple 301 redirect plugin.  When I saw the ‘Change of address’ link in the ‘Configuration’ menu. Done and Done.

Step Five -cPanel

Okay, now I’m serious about all the domains.  And I get to cPanel of our host.  Here is where I start to get an understanding.  All traffic from CFN needs to be ‘redirected’ to WBST.  The domain redirects page seems to be a logical place to do this and sure enough.  using the form I create a redirect that looks like this:

and the .htacess file looks like this now

RewriteEngine on

RewriteCond %{HTTP_HOST} ^capefearnaturalist\.com$ [OR] RewriteCond %{HTTP_HOST} ^www\.capefearnaturalist\.com$
RewriteRule ^(.*)$ “http\:\/\/www\.wrightsvillebeachscenictours\.com\/$1” [R=301,L]

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

# END WordPress

It FEELS like this grabs requests and redirects them BEFORE hitting WordPress and the 301 redirects plugin.  And, it works!  Any inbound links to CFN get redirected to WBST and then redirected, if need be, by the plugin.  404 pages will work.

Stuff you should know

  • Simple 301 redirect plugin – get it. use it. love it.
  • Don’t put spaces in urls / file names – It’s good practice and they won’t get forwarded by 301 redirect
  • cPanel redirects – For the heavy lifting of redirecting
  • Webmaster tools – ‘change of address’ – for when you have duplicate domains in the search (and don’t want that)

Further questions and links

  • What’s the connection with WordPress permalinks?
  • What language does the .htaccess file use?
  • 301 redirect vs WordPress rewrite?
  • Will a new sitemap help when changing over a new site?
  • What cool stuff can you do with .htacess?
  • Can we create a rule or do we have to track down errors one by one?
  • Why does index.html work and home.html not?
  • Use PHP5 Single php.ini as default?
  • $ is the end of a line
  • Where do you put the .htaccess file? the primary domain? the addon domain folder? the folder of the WP install?

Finally

I want to give a super thanks to Dave from addedbytes.com for this excellent article on URL ReWriting for beginners.  If you are looking for the quick and dirty refresher, start there and end with this Cheat Sheet on Mod Rewrite

Leave a Reply