New Bookstore! Save 20% on books with discount code: LAUNCH
Web Dev + WordPress + Security

Permalink Enlightenment

I recently enabled the permalinks feature for a fresh WordPress 2.0.2 upgrade. The process required several hours of research and approximately 90 minutes to fully implement. This brief article summarizes the process and applies to the following setup:

  1. WordPress 2.0.2 or better
  2. Apache Server with mod_rewrite enabled
  3. The ability to modify your site’s .htaccess file(s)

Also, in this tutorial, I will be using the following permalink structure:

/%year%/%monthnum%/%day%/%postname%/

If you are using some other structure, simple replace any instances of the above code with whatever it is you are using.

Before diving in..

Permalinks are important for several reasons: search engines love them, people can understand them, and links (theoretically) become permanent because of them. Plus, all of the cool kids are using them, so you should too. They really add the finishing touches to your site and provide a sense of unity and sophistication that the http://ugly-domain.com/blog/index.php?page_id=66 format just can’t match.

Before you begin any changes it is highly recommended to backup everything: WordPress files, database(s), notes, edited files, image files, htaccess file(s), etcetera. Just a friendly reminder.

Then, if you are seriously considering jumping into permalinks, do some research. Search Google, Ask, WordPress, and dig through a few blogs for terms such as “WordPress permalinks”, “permalink tips”, “permalink guide”, etc. As you learn, copy and paste any important notes, code, or links that seem important and may be useful later on. You may also wish to anticipate certain scenarios by searching for “permalink nightmare” or “permalink success”, for example.

Getting started

Now that you have studied the process, you realize that certain conditions produce errors or other problems. For example, if you are planning on using the %postname% in your permalinks, very long post titles may cause problems in emails, posts, and chats. Although such an issue may be fixed after the fact, it is wise to scan through your posts and edit the post slug of any posts with excessively long titles. Regardless of the post title name, it is the post slug that is used when implementing the %postname% in permalinks. If possible, I recommend manually checking every post title and slug and editing (if needed) with permalinks %postname% in mind. Don’t forget to check your pages as well.

Now for the fun stuff. Well, almost. WordPress 2.0.2 has a problem writing to .htaccess files even when their CHMOD is set to 666 or 777. To fix this, open wp-includes/vars.php and replace the following line of code:

$is_apache = ( strstr($_SERVER['SERVER_SOFTWARE'], 'Apache') || strstr($_SERVER['SERVER_SOFTWARE'], 'LiteSpeed') ) ? 1 : 0;

..with this one:

$is_apache = 1;

This is a change that should be needed only during the establishment of permalinks. After you have permalinks flying high, you should revert the previous replacement and restore order to the galaxy.

Now create your .htaccess file. Open a text editor and save a blank document named htaccess in the root of your site. Then rename it to .htaccess (i.e., put a dot before the name). Upload this file and change its CHMOD setting to 666. Again, this change should only endure until permalinks are functioning. So remember to change it back to its default setting (typically 644). Here is a complete guide to creating .htaccess files.

The fun part

Okay, get ready to party. First restart your computer (for good measure). Then, open your WordPress Admin and navigate to the Permalinks submenu (under Options). Read everything carefully and then select the “Date and name based” option under the “Common options” menu. Make sure that WordPress automatically added /%year%/%monthnum%/%day%/%postname%/ (or that of your preferred format) to the “Custom structure” field. Now click “Update Permalink Structure” and wait for the “success” message to appear. At this point, permalinks should work throughout your WordPress-powered website.

Almost done. Check everything. First download your htaccess file and verify that WordPress added the htaccess directives required to create permalinks:

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

If you don’t see it, then copy and paste it into your htaccess file manually. If your htaccess file is blank, permalinks will not work. Once your htaccess file is good to go, clear your browser cache and surf your entire site — pages, posts — everything. Check internal links and external links. If possible, check links pointing to your site from elsewhere online. Every page or post should now be addressed by its corresponding and unique permalink, as displayed in your browser’s address field. If all is looking smooth, great. Remember to check the functionality of everything: commenting, posting, tagging, and editing posts. Also check your calendar, archives, search function, different themes, and plugin features (if any).

Final notes

One problem I encountered (besides those inherently addressed in this article) involved an unwanted index.php statement included before the date in all permalinks. To fix this, you must access the wp_options table in your WordPress database. Once there, find the index field called option_name and search for the record rewrite_rules and clear the contents of the option_value. This will force WordPress to generate new permalink values based on current user settings. Read more via this thread at the WordPress Support Forums.

Important: Don’t forget to reverse any changes made during the permalink conversion process.

Jeff Starr
About the Author
Jeff Starr = Fullstack Developer. Book Author. Teacher. Human Being.
Digging Into WordPress: Take your WordPress skills to the next level.

8 responses to “Permalink Enlightenment”

  1. This will force WordPress to generate new permalink values based on current user settings

    Absolutely brilliant info, thanks !

    A friend of mine has problem with his WP blog, where the pages didn’t show up.

    When I found your post, I checked that bit above.
    Turned out the size of that record is about 20 KB.

    I emptied that field, and load his website. Took a while for WordPress to recreate its rewrite table – because the result is a 21MB record (yay).

    He’s so happy now. :)

    Thanks again !

  2. Jeff Starr

    Well, I am certainly glad I published this article! I remember fighting this issue for several days before finally discovering a solution. Thanks for the positive feedback — I am stoked that it did the trick for you :)

  3. Don Harder 2008/02/18 2:10 pm

    Oh, this worked beautifully! Thank you!

  4. Jeff Starr

    My pleasure, Don — Glad to be of service! Beautiful site you have there, btw! ;)

  5. I have wordpress 2.3.3 installed. I’m trying to “hide” the location of the directory of where wordpress is located.

    everything in the head has href links which expose the location like
    Gttp://www.club.com/abc/wp-content/etc

    and everything in the rest of the pages, including footer are in the format
    Gttp://www.club.com/wp-content/etc

    Is there something I can do to fix this? Is this a reasonable thing to do to make the site cleaner?

    I’ve used the .htaccess “fix” along with placing a second slightly modified copy of index.php in the “root”.

    thanx!

  6. Jeff Starr
    Perishable 2008/03/22 6:59 pm

    Hi JA,

    Try placing the following code into an htaccess file located in your subdirectory:

    <IfModule mod_rewrite.c>
       RewriteEngine On
       RewriteBase /
       RewriteRule ^abc/(.*)$ /$1 [R=301,NC,L]
    </IfModule>

    ..where abc is the name of your subdirectory. Once in place, this code will redirect all subdirectory requests to corresponding URLs within the root directory of your domain. The net effect of this is the complete removal of the abc (subdirectory) portion of the URL.

  7. I have tried this, and it isn’t working.

    Now I believe you may have misunderstood what it is that I am asking, or I haven’t explained it properly.

    For instance, this line exists on a page, in the

    <script type="text/javascript" src="http://www.club.ca/abc/wp-content/themes/k2/"></script>

    I don’t want the “view source” to show that it is coming from the abc subdirectory.

    or like this…

    I want /abc removed.

    Is it more about me telling these scripts not to look there anymore?

    Now If what you are asking me to do, should work for that, then I havn’t installed it correctly.

  8. Jeff Starr
    Perishable 2008/03/30 2:40 pm

    Hopefully, you realize that htaccess does not rewrite URLs located in the source code of scripts (e.g., JavaScript, PHP, etc.) and markup. You have to edit the code yourself. All the rewriting in the world will not help you change the code in your web pages. Viewing the source code of a document will reveal exactly what has been included in the document itself. I hope this helps, sorry I don’t have time to explain it any further.
    Good luck!

Comments are closed for this post. Something to add? Let me know.
Welcome
Perishable Press is operated by Jeff Starr, a professional web developer and book author with two decades of experience. Here you will find posts about web development, WordPress, security, and more »
The Tao of WordPress: Master the art of WordPress.
Thoughts
Book updates complete! DigWP, .htaccess, Tao-WP, and WP Themes books all updated and current with all the latest.
Stop giving so much juice to social media. Get a site and OWN your content.
I would give my left testicle for macOS Finder to remember column widths.
The chemical name for titin (the largest known protein) has 189,819 characters and takes several hours to pronounce.
Working on book updates, should be available for download sometime next week.
iCloud is like the Terminator. It will never stop trying to get your data. An endless fight on each Apple device to keep iCloud disabled and empty.
Take a screenshot with Firefox (no extension required). Open Developer Tools Settings and enable the “Take a screenshot” button. Then click the button :)
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.