Title: Custom Rewrite rules generated dynamically
Last modified: August 19, 2016

---

# Custom Rewrite rules generated dynamically

 *  [Apex33](https://wordpress.org/support/users/apex33/)
 * (@apex33)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/custom-rewrite-rules-generated-dynamically/)
 * I have a database table containing a set of music bands. Right now I have a page
   template ‘viewband’ set up that takes a query string variable ‘id’ and uses it
   to pull all the info about that band for diaplsy. The URL looks like this:
 * [http://www.mysite.com/viewband/?id=34](http://www.mysite.com/viewband/?id=34)
 * I want to create rewrite rules so that the URL can look like this:
 * [http://www.mysite.com/viewband/the-band-name/](http://www.mysite.com/viewband/the-band-name/)
 * I was thinking it could be done using $wp_rewrite. When a new band is added to
   the database, I could add a new rewrite rule for that band.
    Here is my current
   code that runs when a new band is added to the table:
 *     ```
       function addRewriteRule($slug, $id) {
       	$rewriteSlug = $slug;
       	$rewriteID = $id;
       	add_filter('page_rewrite_rules','rewriteBandPages');
       	flushRules();
       }
   
       function rewriteBandPages($rules) {
       	global $rewriteSlug;
       	global $rewriteID;
       	$newrules = array();
       	$newrules['/viewband/('.$rewriteSlug.')$'] = 'id='.$rewriteID;
       	return $newrules + $rules;
   
       }
   
       function flushRules() {
       	global $wp_rewrite;
       	$wp_rewrite->flush_rules();
       }
       ```
   
 * This isn’t doing anything, however. I get a 404 error when using the URL I wanted
   to create with these functions.
 * Can anyone help me?

The topic ‘Custom Rewrite rules generated dynamically’ is closed to new replies.

## Tags

 * [rewrite rules](https://wordpress.org/support/topic-tag/rewrite-rules/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 0 replies
 * 1 participant
 * Last reply from: [Apex33](https://wordpress.org/support/users/apex33/)
 * Last activity: [15 years, 3 months ago](https://wordpress.org/support/topic/custom-rewrite-rules-generated-dynamically/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
