Title: Dealing with Permalinks and Dynamic Code
Last modified: August 19, 2016

---

# Dealing with Permalinks and Dynamic Code

 *  [buckrogers](https://wordpress.org/support/users/buckrogers/)
 * (@buckrogers)
 * [15 years, 6 months ago](https://wordpress.org/support/topic/dealing-with-permalinks-and-dynamic-code/)
 * I have a question on permalinks.
      I added some code to my template page.php.
   The code checks for a specific page ID then generates dynamic content based on
   two inputs (state and town).   I also have code in .htaccess to rewrite my the
   URL.  I request /consultants/NH/Manchester and it will translate to index.php?
   page_id=100&state=NH&town=Manchester.   If I disable permalinks, this works fine.
   If I enable permalinks, the browser gets a 302 redirect to the actual permalink
   for page_id 100.  In this case /category/consultants-directory.   Can someone
   explain which function executes the 302 redirect.  I can change it to ignore 
   the 302 if the page_id=100.  There could be a better way to do this.  Suggestions?

Viewing 7 replies - 1 through 7 (of 7 total)

 *  [Chrome Orange](https://wordpress.org/support/users/chrome-orange/)
 * (@chrome-orange)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/dealing-with-permalinks-and-dynamic-code/#post-1796287)
 * Did you ever resolve this, I’m trying to do something similar
 * Thanks
 *  Thread Starter [buckrogers](https://wordpress.org/support/users/buckrogers/)
 * (@buckrogers)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/dealing-with-permalinks-and-dynamic-code/#post-1796288)
 * Yes, but it’s not a nice solution. I didn’t have time to figure out how the permalink
   functions work. Or I couldn’t figure it out after a week of late nights. I just
   needed this for one page ID. I changed it in wp-includes/canonical.php. I added
   one line to ignore the function for my page ID. A better way is to use a custom
   field if you have multiple pages you want this for:
 *     ```
       if ( $do_redirect ) {
       		//CHR this line will exclude permalink translation for a single page
                         //Use it to call a page with dynamic page generation code
       	    if ( get_query_var('page_id') == "1137" ) return false;
       		// CHR end modification for 31 directory
   
       		// protect against chained redirects
       		if ( !redirect_canonical($redirect_url, false) ) {
       			wp_redirect($redirect_url, 301);
       			exit();
       		} else {
       			// Debug
       			// die("1: $redirect_url<br />2: " . redirect_canonical( $redirect_url, false ) );
       			return false;
       		}
       	} else {
       		return $redirect_url;
       	}
       ```
   
 * Remember to make a backup copy after the change. The next WordPress upgrade will
   overwrite this.
 *  [Chrome Orange](https://wordpress.org/support/users/chrome-orange/)
 * (@chrome-orange)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/dealing-with-permalinks-and-dynamic-code/#post-1796289)
 * Thank you for the reply. Unfortunately I couldn’t do it your way ao after further
   investigation (ycrta asking on the forum again) my prayers were answered
 * [http://wordpress.org/support/topic/permalinks-and-getting-the-id-which-file?replies=3](http://wordpress.org/support/topic/permalinks-and-getting-the-id-which-file?replies=3)
 * points me to [http://codex.wordpress.org/Function_Reference/WP_Rewrite](http://codex.wordpress.org/Function_Reference/WP_Rewrite)
 * What this means is I can have
 * [http://www.domain.com/your-town-is-TOWNNAME/](http://www.domain.com/your-town-is-TOWNNAME/)
 * convert to [http://www.domain.com/index.php?pagename=your-town-is&town=TOWNNAME](http://www.domain.com/index.php?pagename=your-town-is&town=TOWNNAME)
 * So I only need to write 1 page (your-town-is) and use some shortcode to GET the
   town variable and replace [town] with the variable
 * Perfect solution for me.
 *  Thread Starter [buckrogers](https://wordpress.org/support/users/buckrogers/)
 * (@buckrogers)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/dealing-with-permalinks-and-dynamic-code/#post-1796293)
 * Did you figure this out? It will be very helpful if you explain how you did it.
   I have a similar setup that translate /statelist to index.php?page_id=1137&input1
   =statelist in my .htaccess file. I then have dynamic code in my template page.
   php that generates the content.
 * How do I get rid of the original permalink for page_id=1137?
 * Thanks
    Chris
 *  [Chrome Orange](https://wordpress.org/support/users/chrome-orange/)
 * (@chrome-orange)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/dealing-with-permalinks-and-dynamic-code/#post-1796294)
 * I did it by creating a new function in functions.php and editing the .htaccess
   file
 * for the sake of this lets say that the permalink for your page_id 1137 is STATELIST
   and input1 is OHIO
 * The user would click on a link [http://www.DOMAIN.com/statelist-ohio/](http://www.DOMAIN.com/statelist-ohio/)
 * This goes in functions.php and converts the link into something wordpress can
   use.
 *     ```
       add_filter('rewrite_rules_array','wp_insertMyRewriteRules');
       add_filter('query_vars','wp_insertMyRewriteQueryVars');
       add_filter('init','flushRules');
   
       // Remember to flush_rules() when adding rules
       function flushRules(){
       	global $wp_rewrite;
          	$wp_rewrite->flush_rules();
       }
   
       // Adding a new rule
       function wp_insertMyRewriteRules($rules)
       {
       	$newrules = array();
       	$newrules['(statelist-)(.+?)$'] = 'index.php?pagename=$matches[1]&input1=$matches[2]';
       	return $newrules + $rules;
       }
   
       // Adding the id var so that WP recognizes it
       function wp_insertMyRewriteQueryVars($vars)
       {
           array_push($vars, 'id');
           return $vars;
       }
       ```
   
 * and my .htaccess looks like this
 *     ```
       <IfModule mod_rewrite.c>
   
       	Options +FollowSymlinks
       	RewriteEngine On
       	RewriteOptions MaxRedirects=10
   
       	RewriteBase /SUBFOLDER/
   
       	RewriteRule ^statelist-([^/\.]+)/?$ /SUBFOLDER/index.php?pagename=windows-in&town=$1 [QSA,L]
   
       #	RewriteRule ^index\.php$ - [L]
       	RewriteCond %{REQUEST_FILENAME} !-f
       	RewriteCond %{REQUEST_FILENAME} !-d
       	RewriteRule . /SUBFOLDER/index.php [L]
   
       </IfModule>
       ```
   
 * obviously replace SUBFOLDER as necessary for your setup
 * Now just GET input1 as necessary.
 *  Thread Starter [buckrogers](https://wordpress.org/support/users/buckrogers/)
 * (@buckrogers)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/dealing-with-permalinks-and-dynamic-code/#post-1796297)
 * Great thanks! This worked for me.
 *  [alex chousmith](https://wordpress.org/support/users/chousmith/)
 * (@chousmith)
 * [15 years ago](https://wordpress.org/support/topic/dealing-with-permalinks-and-dynamic-code/#post-1796334)
 * just wanted to say Thanks for this – it totally works, just what i was looking
   for, and is are very slick solution

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Dealing with Permalinks and Dynamic Code’ is closed to new replies.

## Tags

 * [permalink](https://wordpress.org/support/topic-tag/permalink/)
 * [rewriterule](https://wordpress.org/support/topic-tag/rewriterule/)

 * 7 replies
 * 3 participants
 * Last reply from: [alex chousmith](https://wordpress.org/support/users/chousmith/)
 * Last activity: [15 years ago](https://wordpress.org/support/topic/dealing-with-permalinks-and-dynamic-code/#post-1796334)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
