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 https://www.example.com
they have a non-WP platform running at the root which has it’s own .htaccess rules. WP operates from a sub-directory https://www.example.com/blog/. All working perfectly and no problems. They want to write static content as WP pages, but host them at urls like https://www.example.com/advice/advice-page-example/ 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 https://www.example.com/advice/some-page 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.
- The topic ‘Getting a custom rewrite rule for pages to work’ is closed to new replies.