Patrick's Rants



3/16/2005

How I cheated

Filed under: — site admin @ 10:24 pm

I cheated using mod_rewrite and you can too.

Actually, I didn’t really “cheat” so much as hack about with WordPress’ “page” code. As of WP 1.5, a feature called pages was added that allows more statically positioned content, that is it doesn’t roll off the bottom of the page the way the regular blog does. The coders over at wordpress.org wrote some pretty good stuff and if you code your own multiple pages you will want to look at the permalink rewrite code.

My problem is that I don’t want the .htaccess file rewritten (nor anything else in public folders) if I’m writing new pages all the time. Nor do I want to have to redo my wordpress.conf file in the apache configuration directory. Also .htaccess is a little more CPU intensive and I just don’t want it 🙂

So after hours of searching the internet and the support groups for WordPress, I came up with an almost perfect solution.

RewriteRule ^wp-admin/?$ $1/index.php [QSA,L]
RewriteRule ^(.*)/trackback/?$ /index.php?pagename=$1&tb=1 [QSA,L]
RewriteRule ^(.*)/feed/(feed|rdf|rss|rss2|atom)/?$ /index.php?pagename=$1&feed=$2 [QSA,L]
RewriteRule ^(.*)/(feed|rdf|rss|rss2|atom)/?$ /index.php?pagename=$1&feed=$2 [QSA,L]
RewriteRule ^(.*)/page/([0-9]{1,})/?$ /index.php?pagename=$1&paged=$2 [QSA,L]
RewriteRule ^(.+)/$ /index.php?pagename=$1 [QSA,L]

When I say almost perfect, it’s because the link to the other “pages” of this site only work if there is a trailing / but it’s a small price to pay for a little more security, a little more peace of mind and not having to rewrite my configuration file everytime I add a page…


This was broken in 3.1 and is no longer working: http://personal.rwcinc.net/2011/03/20/cheaters-never-win/ Well, six years isn’t a bad run on the internet.

4 Comments

  1. Thanks a lot for this hack.

    Comment by Alex — 10/21/2005 @ 3:50 am

  2. Hack above is good but if your Permalink Structure is “/%postname%/” then you will have problem with posts.

    So my solution is to make all pages link HTML files.

    in .htaccess:
    RewriteRule ^(.*).html/trackback/?$ /index.php?pagename=$1&tb=1 [QSA,L]
    RewriteRule ^(.*).html/feed/(feed|rdf|rss|rss2|atom)/?$ /index.php?pagename=$1&feed=$2 [QSA,L]
    RewriteRule ^(.*).html/(feed|rdf|rss|rss2|atom)/?$ /index.php?pagename=$1&feed=$2 [QSA,L]
    RewriteRule ^(.*).html/page/([0-9]{1,})/?$ /index.php?pagename=$1&paged=$2 [QSA,L]
    RewriteRule ^(.*).html$ /index.php?pagename=$1 [QSA,L]

    And then edit file /wp-includes/template-functions-links.php

    Replace:
    $link = get_settings(‘home’) . “/$link/”;

    With:
    $link = get_settings(‘home’) . “/$link.html”;

    Thank’s All.

    Comment by Alex — 10/21/2005 @ 4:46 am

  3. Indeed, my permalink structure is /archives/date (or something similar) so you might have to adjust the rules to fit into that structure. Like I posted, this keeps me from having to redo my .htaccess file every time I add a page so it works better for me due to that.

    Comment by site admin — 10/22/2005 @ 3:32 pm

  4. Nice site 😉

    Comment by Vasya — 11/13/2006 @ 5:37 am

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.