Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a wordpress page named: directory (it uses a custom template).

When I call the page with:

http://localhost:8888/wordpress/directory/?segments=listings/list

It gives me the results I need. Now I need to make the url cleaner so that it will look like:

http://localhost:8888/wordpress/directory/listings/list

I tried this .htaccess with no luck (I get an internal server error):

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteRule ^directory/?(.*) /wordpress/directory/?segments=$1 [QSA, L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>
# END WordPress

Any ideas how I could get this to work?

EDIT:

Oops, looking more closely at the WP Rewrite Class page noted by Dwayne, I see that the specific example they provided was exactly what I needed. The code was provided on that page after:

A Quick and dirty example for rewriting...

add_filter( 'rewrite_rules_array','my_insert_rewrite_rules' );
add_filter( 'query_vars','my_insert_query_vars' );
add_action( 'wp_loaded','my_flush_rules' );

// flush_rules() if our rules are not yet included
function my_flush_rules(){
    $rules = get_option( 'rewrite_rules' );

    if ( ! isset( $rules['(project)/(\d*)$'] ) ) {
        global $wp_rewrite;
        $wp_rewrite->flush_rules();
    }
}

// Adding a new rule
function my_insert_rewrite_rules( $rules )
{
    $newrules = array();
    $newrules['(project)/(\d*)$'] = 'index.php?pagename=$matches[1]&id=$matches[2]';
    return $newrules + $rules;
}

// Adding the id var so that WP recognizes it
function my_insert_query_vars( $vars )
{
    array_push($vars, 'id');
    return $vars;
}

I just substituted 'directory' for 'project' and 'segments' for 'id' in the example code , and used '(directory)/(.*)$' for the rule to match. I put that in my plugin and it worked just fine.

Thanks for the help.

share|improve this question
    
Note that you should probably not include the "my_flush_rules" piece there, but instead just manually flush the rules by visiting the Settings->Permalinks page. Flushing rules is slow and you also shouldn't need to check for them every load. That example is "dirty" for a reason. –  Otto Nov 22 '13 at 16:17
add comment

1 Answer

As you've discovered Wordpress ignores rewrite rules added into your .htaccess file manually. Not many people actually know Wordpress has in-built functions, a core rewriting class that allows you to add custom rewrite rules into your themes functions.php file.

You can read up on the WP_Rewrite class here and if you do a search on this site you will find numerous questions from people asking to do similar things.

I am a firm believer that writing and posting the code for you is cheating, but if you visit the aforementioned link and do a search you will find all the info you need to implement custom rewrite rules.

Of course if you get stuck, please post the code you do end up writing and I amongst others will gladly help you out and point you in the right direction.

share|improve this answer
    
Thanks, I'm not sure if Wordpress actually ignores rules added to .htaccess manually, because when I manually change, them, they have an effect on what happens. Also, I was adding this through my plugin which flushed and uses: add_rewrite_rule('directory/?(.*)','wordpress/directory/?segments=$1' , 'top' );} I have a feeling the problem might be that Wordpress is internally "rewriting" for the page permalink, so maybe it's causing some kind of loop? I'm not sure how to access where Wordpress is actually rewriting the page slug to index.php?p=123 (123 being the page id). –  Frank Jan 18 '12 at 2:40
1  
Separate rules are not ignored, i've used custom rules alongside WordPress several times. –  t31os May 3 '13 at 10:13
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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