Swimminginthought

A place for all things technical and my mind..

Menu
  • About Me / Hire Me
Menu

Rewriting URLs using Apache and WordPress – Natural Partners

Posted on May 22, 2012 by Percy Kwong

This post discusses how using .htaccess on Apache will actually help you avoid many 404s and also support multiple permalink structures in the process.

.htaccess is very finicky and that in there lies the problem, but with this article, you should be able to solve a few problems (and even do some extreme customization).

My issue was the following:

I monitor my 404 errors on my blog constantly.  This is important as a 404 error basically reports the content is not found.  This can be due to a multitude of issue.  It’s not uncommon to get 1-5% of your errors as 404 errrors.  Anything more than that and the search engines penalize you.

So here’s where I looked at it.  In a typical wordpress installation, people have a tendency to occasionally change their permalinks with no idea what the repercussions are.

So here’s where it gets interesting.  Changing your permalink structure immediately breaks any links to your web content anyone has linked to in the past.

You would think that WordPress would support multiple permalink structures, but it’s not high on their list today.  So how do you support your old links and still serve up that same content using your new link structure, thus avoiding 404 errors?

The answer?  If you’re running Apache is a beautiful tool called mod_rewrite.  I will say that mod_rewrite is extremely finicky and very hard to deal with.  As for the irc support channel, all I can say is it wasn’t a good experience.  If I can read the documentation and rarely make heads or tails of certain key components of it, there’s an issue.

So after much muddling around and experimentation  and testing, I’ve come up with the solution.

So.. I’ll make it simple for you.

I will list the lines you need to put into your .htaccess file (to change your wordpress configuration to support multiple permalink structures.

It will save you.  If you’re using a host, call them up and ask them if they have mod_rewrite activated and available for your use.

If so, then all you have to do is modify your .htaccess file and you’ll be fine.

Here’s what I have in mine:

RedirectMatch 301 ^/([0-9]{4})/([0-9]{2})/(.*)$ http://swimminginthought.com/$3 # This gives you the ability to support the 4-digit-year/2 digit month/post-name permalink structure.
RedirectMatch 301 ^/([0-9]{4})/([0-9]{2})/([0-9]{2})/(.*)$ http://swimminginthought.com/$4 # This covers the 4-digit/2-digit-month/2-digit-day/postname

#Percy

# This ruleset removes the /none from the back of the URL. I had to do this due to a social network’s bad programming.

RewriteBase /
RewriteCond %{REQUEST_URI} none$
RewriteRule ^(.*)/none$ $1 [R=301,L]

#The following stanza below sets my permalink structure to: http://swimminginthought.com/post-name

# 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]
</IfModule>

# END WordPress

 

I sincerely hope this helps you all.  Please remember to comment or link to this page if you like it.  Most importantly, if you find it important, consider donating.  It makes a difference.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Recent Posts

  • Kubernetes install gotcha on CentOS-7.x
  • Infrastructure as Code using AWS Cloudformation
  • Does SSL matter? It does and it’s not just web encryption.
  • Apache can’t serve the .well-known directory
  • Cloudy with a chance of Meatballs
  • Resurrection of the Blog
  • Encryption type Matters with Deep Packet Inspection – solved
  • Completely off topic.. Just a personal note.

Recent Posts

  • Kubernetes install gotcha on CentOS-7.x
  • Infrastructure as Code using AWS Cloudformation
  • Does SSL matter? It does and it’s not just web encryption.
  • Apache can’t serve the .well-known directory
  • Cloudy with a chance of Meatballs

Recent Comments

  • Percy Kwong on Boosting Octane Yourself. I did the research, made it, and proved it.
  • Nick Doelman on Boosting Octane Yourself. I did the research, made it, and proved it.
  • Percy Kwong on International Text solutions – A solution
  • Muzahid Ul Islam on The Browser Wars; Which One is Best?
  • Muzahid Ul Islam on International Text solutions – A solution

Archives

  • September 2019
  • August 2019
  • July 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • August 2011
  • June 2011
  • March 2011
  • February 2011
  • November 2010
  • October 2010
  • September 2010
  • July 2010
  • January 2010
  • November 2009
  • October 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008

Categories

  • Activism
  • All
  • Amazon S3
  • Announcements
  • Apache
  • Apple
  • Applications
  • Automation
  • AWS
  • Azure
  • Backups
  • Blogging
  • Books
  • Bootcamp
  • CentOS – RHEL
  • Cloud Computing
  • Cloudformation
  • Consulting Advice
  • Craigslist
  • Databases
  • Deep Packet Inspection
  • Doing the right thing
  • e-Book Readers
  • E-Mail
  • EC2
  • energy-efficiency
  • environment
  • Expose
  • firewalls
  • General Observations
  • Hardware
  • Hope
  • Hosting
  • IaC
  • Important things to do in life
  • Infrastructure as Code
  • Kubernetes
  • Linux
  • Media and Encoding
  • Miscellany
  • Mobile Phones
  • MySql
  • Networking
  • Performance
  • Personal
  • pfsense
  • Politics
  • Privacy
  • Redundancy
  • Routers
  • security
  • SPAM
  • Tech that doesn't work like it should
  • Tips and Tricks
  • Tools that work
  • Tout
  • Video
  • Virtual Computing
  • VOIP-Voice Over IP
  • VPN
  • Web Browsers
  • Web Hosting
  • Websites
  • Windows
  • Wordpress

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
©2026 Swimminginthought | Built using WordPress and Responsive Blogily theme by Superb