Skip to the content...

Welcome to the David Walsh Blog. I'm a MooTools, Dojo, jQuery, CSS, and PHP Web Developer located in Madison, Wisconsin, United States. Please contact me if I can make your experience on my website better.

WordPress’ .htaccess File Is Genius

17 Responses »

Choosing to use WordPress was a more difficult decision that you may think. Sure, WordPress is the most used blogging software on the internet, but I'm a programmer, right? I should want to code everything myself, right? I thought that at first but I decided to try WordPress first and I've been impressed with every part of WordPress so far. One of the interesting parts of WordPress is the URL rewriting and "slug" that gets created for each post. How did they do that?

Well, I know enough about SEF (search engine friendly) links to know that an .htaccess file had to be involved so I downloaded the .htaccess file from my hosting server and saw:


RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

That's it? Yep. Basically, the flow goes as follows:

  1. We must first establish that mod_rewrite is available on the server
  2. If so, turn on mod_rewrite
  3. Set the base of all rewriting to the web root folder
  4. If the requested filename isn't a file....
  5. ....and it isn't a folder...
  6. Send the person to index.php

Once inside the index.php file, the index.php file processes the request and presents you with the page based upon the slug in the URL. Very simple .htaccess code runs a reliable WordPress blog.

Discussion

  1. October 5, 2007 @ 9:51 am

    I have been playing around with this in WordPress, and the only problem I am still having is that the “Next Page” link at the bottom of some pages does not work correctly. The link goes to http://domain.com/page/2/ and is supposed to use mod_rewrite to go to ./index.php?page=2 but all attempts I make to resolve this have not helped.

    I am currently trying something like:
    “RewriteRule ^/page/(\d+)/? /index.php?page=$1″

    What say you? Thanks for your help.

  2. October 5, 2007 @ 4:28 pm

    Eric, do you use WordPress 2.3 + the “Permanent Redirect”-Plugin?
    I did and came upon the exact same problem. Deactivating the plugin solved this, WP2.3 will do for you with their canonical URLs now.

  3. October 5, 2007 @ 5:16 pm

    @Eric:

    Odd, my “next page” functionality has always worked so I can’t image what the issue is. Is your regular expression working correctly?

    @Marco:

    Thanks for contributing!

  4. October 6, 2007 @ 10:15 am

    Yes, actually turning off permalinks solves the problem. I was really hoping to figure this out so I don’t have ?p=4 stuff.

    I don’t think my regex is working. Maybe I will try to make it more general (like \w+/page/(\d+)/?). Do I need the ^ and $ in my regex or will it be more flexible (albeit less secure)?

  5. October 8, 2007 @ 3:15 pm

    Setting up RewriteRules like this to push everything through one file, and processing it according to the structure of the url is a pretty common practice in most mvc frameworks. Its great for seo, and great for developing a clean mvc based site.

  6. October 8, 2007 @ 3:32 pm

    Thanks for commenting Brad.

    I’m aware that setting up RewriteRules is the way to do things, but WordPress’ .htaccess file is so simple.

    I’d post RewriteRules I’ve written to do the same function (MVC) but I’m too embarrassed!

  7. October 16, 2007 @ 9:54 pm

    Heres a couple .htaccess code snippets I use to tweak wordpress..

    RewriteCond %{REQUEST_URI} ^[^\.]+\.html/$
    RewriteRule ^(.*)\.html.*$ http://www.askapache.com/$1.html [R=301,L]

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(feed|wp-atom|wp-feed|wp-rss|wp-rdf|wp-commentsrss)(.*)\ HTTP/ [NC,OR]
    RewriteCond %{QUERY_STRING} ^feed [NC]
    RewriteCond %{HTTP_USER_AGENT} !^(FeedBurner|FeedValidator|talkr) [NC]
    RewriteRule .* http://feeds.askapache.com/apache/htaccess? [R=307,L]

    And I use a ton of RedirectMatch’s

  8. October 16, 2007 @ 9:56 pm

    I use a ton of RedirectMatch’s and a few other .htaccess tweaks like:

    RewriteCond %{REQUEST_URI} ^[^\.]+\.html/$
    RewriteRule ^(.*)\.html.*$ http://www.askapache.com/$1.html [R=301,L]

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(feed|wp-atom|wp-feed|wp-rss|wp-rdf|wp-commentsrss)(.*)\ HTTP/ [NC,OR]
    RewriteCond %{QUERY_STRING} ^feed [NC]
    RewriteCond %{HTTP_USER_AGENT} !^(FeedBurner|FeedValidator|talkr) [NC]
    RewriteRule .* http://feeds.askapache.com/apache/htaccess? [R=307,L]

  9. September 26, 2008 @ 8:31 am

    Hai David

    Can you help? how to .httaccess in Local pc ??

    i am using wamp server

  10. November 15, 2008 @ 6:36 am

    As Brad said, this is a pretty stock-standard .htaccess file. I know that CakePHP uses exactly the same (possibly minus the RewriteBase directive) and a number of other CMSs and frameworks do also. This set of rules is all most sites need to be able to support pretty URLs and a sensible code structure.

    So yes, WP’s .htaccess is elegant in its simplicity, but it’s hardly alone!

  11. November 15, 2008 @ 4:41 pm

    chirag: If you have AllowOverride All in you httpd.conf file then you can place the .htaccess file in the same folder you have your index.php.
    If not you either set the AllowOverride to All (Note: There might be another option to just allow the .htaccess file to work but I normally have it set to All) or add the changes in the httpd.conf

    Nice article david. I too use those lines to redirect everything to index.php in my php mvc framework :)
    Never really knew exactly what “RewriteCond %{REQUEST_FILENAME} !-f” and
    “RewriteCond %{REQUEST_FILENAME} !-d” really meant.
    Thanks for clearing that up!

  12. February 18, 2009 @ 6:35 pm

    Do you know what it passes back to the server?

  13. September 26, 2009 @ 5:08 am

    @Brandon Hansen: take a look at $_SERVER['REQUEST_URI'] and the query.php file near all is_home etc…

  14. October 24, 2009 @ 11:44 am

    yes, no doubtful…mens behind WP are genius!!!

    anyway, any tutorial how to proccesing request url in wordpress, i mean in index.php?

    regards

  15. nirok
    February 12, 2010 @ 3:32 pm

    I’ve been trying to find an answer everywhere for this but how do you get the reply box on the comments like you have here?

  16. jonathan
    May 14, 2010 @ 4:12 pm

    Hi,

    You look like a htaccess knowledgeable person, I have a riddle for you:

    So we’ve installed WordPress into our /blogs/ folder.

    We had an old store in the /store/ folder.

    We upgraded to WP E-Commerce and now it’s in /blogs/store/

    We want to make all the old links that go to /store/ load the new store.

    I’ve tried this but it does not work:

    RewriteRule ^store/ /blogs/store/

    or

    RewriteRule ^store /blogs/?p=123

    if 123 is the page id of store.

Be Heard!

Share your thoughts with fellow developers of all skill levels! I want to hear from you!

Name*:
Email*:
Website:  
Wrap your code with <code> tags, f00!