Forum Moderators: phranque

Message Too Old, No Replies

Trying to add a custom rewrite rule to Wordpress rewriterules

Want to rewrite a custom URL for a wordpress theme

         

hikerjoe

3:19 am on Nov 24, 2009 (gmt 0)

10+ Year Member



I didn't find any other articles pertaining specifically to this. I am creating a custom theme for Wordpress that needs to extend the page.php template type to accept a querystring representing a custom data type - which I've already got working - however, I want to make the resulting URL into a "friendly" url.

This is how it works right now:

http://www.myblog.com/mybooks/?book=catch-22

Which loads my custom theme template and displays the data corresponding to the book parameter - all based on PHP script I have in that template.

What I would like, however, is to rewrite a "friendly" url like this:

http://www.myblog.com/mybooks/book/catch-22

How would I add the rule I need to the existing Wordpress RewriteRule?

This is the default Wordpress rule:


RewriteEngine On
RewriteBase /myroot/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /myroot/index.php [L]

And this rule only works in a separate test page independent of Wordpress:

RewriteEngine On
RewriteRule ^book/([^/\.]+)/?$ index.php?book=$1 [L]

How can I combine both? Nothing I've tried so far has worked. I get either a 500 Server Configuration error or simply a 404 error.

Any help is greatly appreciated.

jdMorgan

1:46 pm on Nov 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your rule will reject /mybooks/book/catch-22 because its pattern specifically disallows slashes in the URL-path.

Is the /mybooks URL-path within or below the /myroot path?

If the new rule is to work, it will need to go above the Wordpress rule, either in the same .htaccess file, in a .htaccess or config file above the Wordpress .htaccess file's directory, or in a path below root that is different from the path to the WP .htaccess file. In other words, the new rule must execute either first or exclusively.

If you get a 500-Server error, look at your server error log. It will often tell you what's wrong, and that information is useful to both you and to readers here.

Jim

hikerjoe

7:34 pm on Nov 24, 2009 (gmt 0)

10+ Year Member



Hi Jim,

Thank you very much for taking the time to respond. This has been a difficult nut to crack, but after a lot more searching a bit of good luck, I figured it out.

It turns out to have had nothing to do with modifying the rewrite rules in .htaccess after all. Instead, the solution was to the modify the query rules Wordpress uses internally to handle custom queries when sent as "friendly" URLs (or "Permalinks") by way of the $wp_rewrite function.

The answer that worked for me can be found in the Wordpress Codex document entitled "Custom Queries [codex.wordpress.org]," right at the bottom under the heading "Permalinks for Custom Archives." I just took the example in that article, added it to my plug-in script, and it worked.