I have a script that I wrote some time ago, and have just decided to "port" it into a WP plugin.

Everything works great, except for one piece of vital core functionality.

The script uses mod_rewrite to allow for virtual URLs to be used, that include a "keyword" that the script then uses later for other functions.

In particular, it allows for URLs like this:


it will then use "page-name" to generate the actual page content, and "my-keyword" is passed to other functions in the script for other purposes.

If WP is on default permalinks, then everything works just fine. But as soon as "pretty permalinks" are in use, my rewrite rules conflicts with the WP rules, and this feature ceases to work. WP instead generates a 404 as you would probably expect, since the requested page URL doesn't exist.

So I need a way to have custom permalinks enabled, such as /%postname%/ for example, but still be able to add information after the post name in the URL. My original script could even do this:


and it would still use "page-name" for the content, and "my-keyword" as the keyword, ignoring everything between. I would like this functionality with WP as well but as yet have not been able to figure out the solution.

Here's my script's original rewrite code:

RewriteBase /wp
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [S=44]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [S=44]
RewriteRule ^(.*)$ index.php?k=$1&%{QUERY_STRING}

And here is what WP generates by default when I set my permalinks to /%postname%/:

RewriteBase /wp/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wp/index.php [L]

I would prefer to not require the user to do any htaccess editing, handling this instead through my plugin itself if possible. Have gotten close but not quite there.

Any ideas?




If the user decides not to enable pretty permalinks, then I would respect their decision not to enable them.

Two potential options:

  1. Simply state that the Plugin requires pretty permalinks to be enabled in order to work
  2. As a fallback, just append the keyword as a query string if pretty permalinks are not enabled.


Perhaps try this tutorial for extending the WP rewrite rules?

Basically, the rewrite rules are an array, so you crate a function that pulls in the array, manipulate it, and then return it; then you hook that array into the generate_rewrite_rules hook.

You might also reference what the WP Access Control Plugin does.

  • I'm afraid the point has been missed. The feature that my users specifically WANT is to pass their keyword in the URL itself, not a query string parameter. But in WP the plugin has to respect that they may have a different default permalinks structure. I can't over-rule what they have set. I don't actually care what their permalinks setting is, I just need the plugin to work as designed with regards to the incoming keyword. – Jonathan Jul 3 '11 at 21:23
  • I'm afraid my point has been missed: if users want pretty permalinks, then they need to enable pretty permalinks - whether the rendered permalink is the WordPress-generated permalink, or your appended keyword. – Chip Bennett Jul 3 '11 at 21:47
  • I'm afraid it's not that simple, perhaps I need to explain more clearly. If a user installs my plugin on an existing WP site that uses a custom "pretty" permalink structure, then they cannot DISABLE their pretty permalinks just to satisfy my plugin. The plugin's requirements are completely separate from the rest of their site. It needs its OWN permalinks rule, that operates entirely independently of the rest of the site. – Jonathan Jul 3 '11 at 22:02
  • Okay then, perhaps I misunderstood your original question. I thought you were saying that the problem existed when users did not have pretty permalinks enabled, and that it was already working in the case in which the user does have pretty permalinks enabled. Was that a misunderstanding? – Chip Bennett Jul 3 '11 at 22:05
  • Yes indeed, it's the reverse. If default permalinks are chosen, then my original htaccess code works fine (since there is no other code to conflict with). But once a different structure is chosen, then my code conflicts. I would like to address this in PHP rather than mod_rewrite hacking, and it does appear there are some functions & hooks to do so, but they're a little over my head... I'm looking at codex.wordpress.org/Class_Reference/WP_Rewrite but can't really grasp how to work with it on the fly like I'm needing. – Jonathan Jul 3 '11 at 22:11

As an addition to @Chip Bennets suggestion:

You can check the Option Reference to check for pretty permlinks/permalink structure.

There's a built in function add_query_arg() that allows to append to the original query string.

  • Interesting... I'm not sure that would really help in my case however, since the problem is that when my plugin is being used as intended, there is no query string, the user simply passes the keyword as part of the URL itself. So there is no "original query string" to append to... – Jonathan Jul 3 '11 at 21:45
  • The original query string is the existing one (you have to have on - else nothing gets displayed). Maybe you want to take a look at Jan Fabrys WPSE Rewrite Analyze Plugin in our Repo. – kaiser Jul 3 '11 at 22:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.