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.
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.