Support » Fixing WordPress » Getting a custom rewrite rule for pages to work

  • First off, this is my first WP support post so apologies if I’m missing something. So:

    I am building a clients site at

    they have a non-WP platform running at the root which has it’s own .htaccess rules. WP operates from a sub-directory All working perfectly and no problems. They want to write static content as WP pages, but host them at urls like etc.

    NB – the directory /advice does not actually exist on the server

    Progress so far: I’ve written a bypass rule in the root htaccess, to pass any url containing /advice/ to /blog/advice/:

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_URI} ^.+advice.+$
    RewriteRule  ^advice/(.+)/?$ blog/advice/$1

    This seems to work in that I get a WP themed response, all be it a 404. I’ve tried a variety of rules in the WP htaccess to get the advice pages to show up, with no luck. When I study the $wp->matched_rule it seems to be matching an attachment request and 404ing.

    So I inserted a custom rule into the rewrite_rules_array to match:

    $newrules['advice/(.+)$'] = 'index.php?pagename=$matches[1]';

    My request for still receives a WP 404, but the rule is being matched:

    <!-- Request: advice/some-page -->
    <!-- Matched Rewrite Rule: advice/(.+)$ -->
    <!-- Matched Rewrite Query: pagename=some-page -->

    As an interim I’ve had to change the root htaccess rule to a redirect:

    RewriteRule ^advice/(.+)/?$ blog/advice/$1 [R] so that at least the pages are served, but i’m sure i must be missing something obvious to get this working. Any help gratefully received and feel free to mock my dumb mistakes.

Viewing 3 replies - 1 through 3 (of 3 total)
  • I’m clearly not as experienced with rewrite rules as you, but I seem to remember that the best way to add rewrite rules is add_rewrite_rule($rule, $rewrite, $position);Function Reference

    The $positoin variable allows you to specify ‘top’, so that these rules are met before others, and will override any potential conflicts.

    Also, a little rusty on the rules, but wouldn’t it be 'index.php?p=$matches[1]'?

    thanks – you may be right about the best way to add rules. i’ll certainly try it that way, but since the rule is apparently being matched i’m not sure it’s the way i’m inserting it that’s the problem.

    'index.php?p=$matches[1]' – you may be right. I’ll check it out. i know if i enter the pagename=some-page URL directly, it works but that may be a coincidence!

    If I use p=$matches[1] the WP home page is displayed, with the following:

    <!-- Request: advice/some-page -->
    <!-- Matched Rewrite Rule: advice/(.+)$ -->
    <!-- Matched Rewrite Query: p=some-page -->
    <!-- Loaded Template: index.php -->

    so not a solution I’m afraid. It’s not a 404 admittedly, but it’s also not the page requested.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Getting a custom rewrite rule for pages to work’ is closed to new replies.