Permalink Enlightenment

Post #90 categorized as Websites, WordPress, last updated on Apr 6, 2008
Tagged with apache, code, htaccess, permalink, php, upgrade, WordPress

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 at least the following setup:

  1. WordPress 2.0.2
  2. Apache Server with mod_rewrite enabled
  3. The ability to access/modify your .htaccess file(s)
  4. You have decided to use the /%year%/%monthnum%/%day%/%postname%/ permalink format (actually, any format will work; simply swap your preferred format for the one mentioned here)
  5. You may also be using any of the plugins listed in the dungeon (actually, most plugins are compatible — only a few cause problems)

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.

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(s). Open a text editor and save a blank document as “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).

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 the “Update Permalink Structure” button 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 check that WordPress automatically added the htaccess directives required to create permalinks:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /press/
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).

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.

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

References & Resources

Subscribe to Perishable Press


8 Responses

TopLeave a comment

[ Gravatar Icon ]

#1sufehmi

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 !

[ Gravatar Icon ]

#2Perishable

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 :)

[ Gravatar Icon ]

#3Don Harder

Oh, this worked beautifully! Thank you!

[ Gravatar Icon ]

#4Perishable

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

[ Gravatar Icon ]

#5JA

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!

[ Gravatar Icon ]

#6Perishable

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.

[ Gravatar Icon ]

#7JA

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.

[ Gravatar Icon ]

#8Perishable

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!

Share your thoughts..

TopRead official comment policy

The rules are simple. Comment intelligently. Stay on-topic. Don’t spam! Suspected spam will be deleted. Use your real name or nickname, not a site name or business name. Using a site name or business name is a good way to get your link or comment removed. Certain comments are moderated; if your comment does not appear after several days, or if you wish to comment privately, contact me. Also, by posting a comment, you grant this site a perpetual license to reproduce your comment, name, and website URL. Lastly, you may use basic HTML markup, but please do not use <pre> tags. Instead, wrap your code with <code> tags. Use a new set of <code> tags for each code term or phrase, as well as for each individual line of code (i.e., multiple lines of code require multiple code tags). Please see the complete comment policy for more information.