Question

I'm currently developing a WordPress site that has an unusual taxonomy. In order to get around certain issues I need to add a parameter to the end of the URL. I have this working, however I need some way of using mod_rewrite to make the parameter pretty.

Here is what I currently have...

www.site.com/('director-name' or 'project-name')/?path=('visual' or 'production')

for example: www.site.com/joe-smith/?path=visual

I am using the $_GET to access this variable for my functionality, and it works fine. The issue is I want to make the URL read...

www.site.com/joe-smith/visual OR www.site.com/art/production

I have read that messing with the .htaccess in WordPress isn't the best way to deal with this.

Help will be much appreciated. Thanks!

Was it helpful?

Solution

You don't need to do anything with .htaccess or mod_rewrite since you are using Wordpress. Wordpress has a built in system for handling rewrite rules. You can also set up your URL parameters right in the function. The function is add_rewrite_rule() and the example in the codex is pretty good. I can walk you through it, though, so you can see exactly what is going on.

Take the following rewrite rule from the example:

add_rewrite_rule('^nutrition/([^/]*)/([^/]*)/?','index.php?page_id=12&food=$matches[1]&variety=$matches[2]','top');

First, lets go over the function arguments. The first argument is the regex for your rewrite. If this regex finds a match on the current URL, your rewrite will kick in. Any URL parameters will be loaded into a $matches array to be used later in the second argument of the function.

The second argument is actual URL that will be rewritten. Note that the page_id is being used here and not the page slug. It is important that you use the page ID for this parameter and not the slug since the slug itself is a rewrite. The latter parameters are the values for the querystring that you are passing to your handler.

As you may know, all pages in Wordpress are routed through the index.php file. If you are familiar with MVC this is very much like the front controller. As such, all of your rewrites should contain the index.php file at the beginning. This is reflected in the second argument in this function. You can have custom rewrites that use other files, but for the sake of simplicity, let's assume that you are using the standard Wordpress index.php file.

The final argument is the position in the queue that the rewrite should be placed. You can either use "top" or "bottom". Top being loaded before Wordpress' rewrites and bottom being after.

Using this function Wordpress will add lines to your htaccess on-the-fly and update them based on the current page. This is definitely the best way to do rewrites in Wordpress! Hope this helps!

UPDATE BASED ON COMMENTS

You would place this code in your functions.php file. You should wrap it in a function and hook it to an action that fires before the rewrite rules. Here is an example that matches your exact issue:

add_action('init', 'my_custom_rewrite_function');
function my_custom_rewrite_function() {
    $director_category_id = 5; // Change to correct id
    $project_category_id = 6; // Change to correct id
    $handler_page_id = 25 // The page ID for the handler

    add_rewrite_rule('^director/([^/]*)/([^/]*)/?','index.php?page_id=$handler_page_id&category=$director_category_id&name=$matches[1]&path=$matches[2]','top');
    add_rewrite_rule('^project/([^/]*)/([^/]*)/?','index.php?page_id=$handler_page_id&category=$project_category_id&name=$matches[1]&path=$matches[2]','top');

    flush_rewrite_rules(); // This will make sure that the rewrite rules are added
}

You can then handle your query in your custom template by doing a get_query_var() for each of your querystring arguments.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top