{"id":61,"date":"2005-07-08T17:24:15","date_gmt":"2005-07-08T07:24:15","guid":{"rendered":"http:\/\/www.thunderguy.com\/semicolon\/2005\/07\/08\/wordpress-rewrite-rules\/"},"modified":"2005-07-13T17:21:52","modified_gmt":"2005-07-13T07:21:52","slug":"wordpress-rewrite-rules","status":"publish","type":"post","link":"https:\/\/thunderguy.com\/semicolon\/2005\/07\/08\/wordpress-rewrite-rules\/","title":{"rendered":"WordPress rewrite rules"},"content":{"rendered":"<p>This is an investigation into some problems with the way WordPress generates <code>.htaccess<\/code> rules to make its fancy permalinks work. For a new site I am building, I wanted the permalinks to not have the date in the URL. Accordingly, I set the permalink structure to the following.<\/p>\n<pre class=\"code\"><code>\/%category%\/%postname%\/<\/code><\/pre>\n<p>Once I did this, I couldn&#8217;t access any single posts. <!--more-->The <a href=\"http:\/\/codex.wordpress.org\/Permalink_Structure\">Codex warns about related issues<\/a>:<\/p>\n<blockquote><p><strong>Note on using only %postname% <\/strong><br \/>\nIf you use postname as the only element in your permalinks to create a structure such as myblog.com\/post-title, the rewrite rules may make it impossible to access pages such as your stylesheet (which has a similar format) or the wp-admin folder. It&#8217;s best to include some numeric data (e.g. the post ID or date) in the permalink to prevent this from happening.<\/p><\/blockquote>\n<blockquote><p><strong>Note on using %category% <\/strong><br \/>\n%category% does not work correctly with mod_rewrite in Apache versions prior to 2. If you are using Apache 1, do not use %category% in your permalink structure.<\/p><\/blockquote>\n<p>Hmmm. My server uses Apache 1, and I had no problems with %category%, but maybe this is related to my problem. The next step was to look inside the <code>.htaccess<\/code> file to see what was going on. Here are the last few rules WordPress generated.<\/p>\n<pre class=\"code\"><code>RewriteRule ^(.+)\/feed\/(feed|rdf|rss|rss2|atom)\/?$ \/blog\/index.php?category_name=$1&amp;feed=$2 [QSA,L]\r\nRewriteRule ^(.+)\/(feed|rdf|rss|rss2|atom)\/?$ \/blog\/index.php?category_name=$1&amp;feed=$2 [QSA,L]\r\nRewriteRule ^(.+)\/page\/?([0-9]{1,})\/?$ \/blog\/index.php?category_name=$1&amp;paged=$2 [QSA,L]\r\nRewriteRule <span style=\"color:red\">^(.+)\/?$<\/span> \/blog\/index.php?category_name=$1 [QSA,L]\r\nRewriteRule ^(.+)\/([^\/]+)\/feed\/(feed|rdf|rss|rss2|atom)\/?$ \/blog\/index.php?category_name=$1&amp;name=$2&amp;feed=$3 [QSA,L]\r\nRewriteRule ^(.+)\/([^\/]+)\/(feed|rdf|rss|rss2|atom)\/?$ \/blog\/index.php?category_name=$1&amp;name=$2&amp;feed=$3 [QSA,L]\r\nRewriteRule ^(.+)\/([^\/]+)\/page\/?([0-9]{1,})\/?$ \/blog\/index.php?category_name=$1&amp;name=$2&amp;paged=$3 [QSA,L]\r\nRewriteRule ^(.+)\/([^\/]+)(\/[0-9]+)?\/?$ \/blog\/index.php?category_name=$1&amp;name=$2&amp;page=$3 [QSA,L]\r\nRewriteRule ^(.+)\/([^\/]+)\/trackback\/?$ \/blog\/index.php?category_name=$1&amp;name=$2&amp;tb=1 [QSA,L]<\/code><\/pre>\n<p>The first four rules in this excerpt deal with category requests, and the rest deal with single-post requests. Note that the fourth rule in this excerpt matches everything, so the subsequent single-post rules are never used. This explained the problem.<\/p>\n<p>The solution is simple: just move the four category rules to the end of the block. That fixes the problem, but it means I will have to manually fix the file every time WordPress regenerates the rewrite rules. This happens every time you create or edit a page, and maybe at other times too. What a pain.<\/p>\n<p>Mysteriously, this problem did not happen on my test server. There, WordPress put the four category rules after the single-post rules automatically, so there were no problems. There are many differences between my test and live servers, so I&#8217;ll need to figure out which differences are significant.<\/p>\n<p>My next step will be to look into how WordPress generates its rules. It would be nice to remove any restrictions on permalink structures.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is an investigation into some problems with the way WordPress generates .htaccess rules to make its fancy permalinks work. For a new site I am building, I wanted the permalinks to not have the date in the URL. Accordingly, I set the permalink structure to the following. \/%category%\/%postname%\/ Once I did this, I couldn&#8217;t [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[83],"class_list":["post-61","post","type-post","status-publish","format-standard","hentry","category-wordpress","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/posts\/61","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/comments?post=61"}],"version-history":[{"count":0,"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/posts\/61\/revisions"}],"wp:attachment":[{"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/media?parent=61"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/categories?post=61"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/tags?post=61"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}