Title: add_rewrite_rule
Published: April 25, 2014
Last modified: August 20, 2026

---

# add_rewrite_rule( string $regex, string|array $query, string $after = 'bottom' )

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/add_rewrite_rule/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/add_rewrite_rule/?output_format=md#parameters)
 * [More Information](https://developer.wordpress.org/reference/functions/add_rewrite_rule/?output_format=md#more-information)
 * [Source](https://developer.wordpress.org/reference/functions/add_rewrite_rule/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/add_rewrite_rule/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/add_rewrite_rule/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/add_rewrite_rule/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/add_rewrite_rule/?output_format=md#wp--skip-link--target)

Adds a rewrite rule that transforms a URL structure to a set of query vars.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/add_rewrite_rule/?output_format=md#description)󠁿

Any value in the $after parameter that isn’t ‘bottom’ will result in the rule being
placed at the top of the rewrite rules.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/add_rewrite_rule/?output_format=md#parameters)󠁿

 `$regex`stringrequired

Regular expression to match request against.

`$query`string|arrayrequired

The corresponding query vars for this rewrite rule.

`$after`stringoptional

Priority of the new rule. Accepts `'top'` or `'bottom'`. Default `'bottom'`.

Default:`'bottom'`

## 󠀁[More Information](https://developer.wordpress.org/reference/functions/add_rewrite_rule/?output_format=md#more-information)󠁿

[add_rewrite_rule()](https://developer.wordpress.org/reference/functions/add_rewrite_rule/)
allows you to specify additional rewrite rules for WordPress. It is most commonly
used in conjunction with [add_rewrite_tag()](https://developer.wordpress.org/reference/functions/add_rewrite_tag/)(
which allows WordPress to recognize custom post/get variables).

## 󠀁[Source](https://developer.wordpress.org/reference/functions/add_rewrite_rule/?output_format=md#source)󠁿

    ```php
    function add_rewrite_rule( $regex, $query, $after = 'bottom' ) {
    	global $wp_rewrite;

    	$wp_rewrite->add_rule( $regex, $query, $after );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/rewrite.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/rewrite.php#L140)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/rewrite.php#L140-L144)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/add_rewrite_rule/?output_format=md#related)󠁿

| Uses | Description | 
| [WP_Rewrite::add_rule()](https://developer.wordpress.org/reference/classes/wp_rewrite/add_rule/)`wp-includes/class-wp-rewrite.php` |

Adds a rewrite rule that transforms a URL structure to a set of query vars.

  |

| Used by | Description | 
| [WP_Sitemaps::register_rewrites()](https://developer.wordpress.org/reference/classes/wp_sitemaps/register_rewrites/)`wp-includes/sitemaps/class-wp-sitemaps.php` |

Registers sitemap rewrite tags and routing rules.

  | 
| [WP_Post_Type::add_rewrite_rules()](https://developer.wordpress.org/reference/classes/wp_post_type/add_rewrite_rules/)`wp-includes/class-wp-post-type.php` |

Adds the necessary rewrite rules for the post type.

  | 
| [rest_api_register_rewrites()](https://developer.wordpress.org/reference/functions/rest_api_register_rewrites/)`wp-includes/rest-api.php` |

Adds REST rewrite rules.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/add_rewrite_rule/?output_format=md#changelog)󠁿

| Version | Description | 
| [4.4.0](https://developer.wordpress.org/reference/since/4.4.0/) | Array support was added to the `$query` parameter. | 
| [2.1.0](https://developer.wordpress.org/reference/since/2.1.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/add_rewrite_rule/?output_format=md#user-contributed-notes)󠁿

 1.    [Skip to note 12 content](https://developer.wordpress.org/reference/functions/add_rewrite_rule/?output_format=md#comment-content-3503)
 2.     [Anthony Eden](https://profiles.wordpress.org/anthonyeden/)  [  7 years ago  ](https://developer.wordpress.org/reference/functions/add_rewrite_rule/#comment-3503)
 3.   [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_rewrite_rule%2F%23comment-3503)
      Vote results for this note: 21[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_rewrite_rule%2F%23comment-3503)
 4.   Here is a simple example of how to register a new rewrite rule, and pass it off
      to a PHP file for rendering:
 5.   1. Setup a rule:
 6.       ```php
          add_action( 'init',  function() {
              add_rewrite_rule( 'myparamname/([a-z0-9-]+)[/]?$', 'index.php?myparamname=$matches[1]', 'top' );
          } );
          ```
      
 7.   2. Flush permalinks. Go to WP Admin > Settings > Permalinks > Save. This doesn’t
      happen automatically after you add this code
 8.   3. Whitelist the query param:
 9.       ```php
          add_filter( 'query_vars', function( $query_vars ) {
              $query_vars[] = 'myparamname';
              return $query_vars;
          } );
          ```
      
 10.  4. Add a handler to send it off to a template file:
 11.      ```php
          add_filter( 'template_include', function( $template ) {
              if ( get_query_var( 'myparamname' ) == false || get_query_var( 'myparamname' ) == '' ) {
                  return $template;
              }
      
              return get_template_directory() . '/template-name.php';
          } );
          ```
      
 12.   * I added a modification for #4 to fix the 404 page issue.
       * add_action( ‘template_include’, function( $template ) { if ( get_query_var(‘
         myparamname’ ) == false || get_query_var( ‘myparamname’ ) == ” ) { return 
         $template; } if ( [is_404()](https://developer.wordpress.org/reference/functions/is_404/)){
         status_header( 404 ); get_template_part( 404 ); exit(); } return [get_template_directory()](https://developer.wordpress.org/reference/functions/get_template_directory/).‘/
         template-name.php’; } );
       * [signmein](https://profiles.wordpress.org/signmein/) [6 years ago](https://developer.wordpress.org/reference/functions/add_rewrite_rule/#comment-4006)
       * When defining the RegExp (step 1), I had to change `index.php?myparamname=
         $matches[1]` to `index.php?myparamname=$1`, otherwise it was not working properly.
       * [elcondorito](https://profiles.wordpress.org/elcondorito/) [5 years ago](https://developer.wordpress.org/reference/functions/add_rewrite_rule/#comment-5745)
 13.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_rewrite_rule%2F%3Freplytocom%3D3503%23feedback-editor-3503)
 14.   [Skip to note 13 content](https://developer.wordpress.org/reference/functions/add_rewrite_rule/?output_format=md#comment-content-2038)
 15.    [tomasdev](https://profiles.wordpress.org/tomasdev/)  [  10 years ago  ](https://developer.wordpress.org/reference/functions/add_rewrite_rule/#comment-2038)
 16.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_rewrite_rule%2F%23comment-2038)
      Vote results for this note: 13[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_rewrite_rule%2F%23comment-2038)
 17.  Things that were in WP Codex site and did not get migrated for whatever reason.
 18.  **NOTE**: When using $matches[] to retrieve the values of a matched URL, capture
      group data starts at 1, not 0.
 19.  **IMPORTANT**: Do not forget to flush and regenerate the rewrite rules database
      after modifying rules. From WordPress Administration Screens, Select Settings-
      > Permalinks and just click Save Changes without any changes.
 20.   * When adding multiple rules, note that (as of 4.9.8) rules are applied in the
         order added, _regardless of whether they’re at the top or the bottom_ E.g.:
       *     ```php
             add_rewrite_rule('^animals/(.*)', 'index.php?pagename=display-animal&amp;animal=matches[1]', 'top'); add_rewrite_rule('^animals/mammals/(.*)', 'index.php?pagename=display-mammal&amp;mammal-name=matches[1]', 'top');
             ```
         
       *  The second rule will never match, because the first rule always matches before
         the second one is processed.
       * [jbeninger](https://profiles.wordpress.org/jbeninger/) [7 years ago](https://developer.wordpress.org/reference/functions/add_rewrite_rule/#comment-3336)
 21.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_rewrite_rule%2F%3Freplytocom%3D2038%23feedback-editor-2038)
 22.   [Skip to note 14 content](https://developer.wordpress.org/reference/functions/add_rewrite_rule/?output_format=md#comment-content-1629)
 23.    Anonymous User  [  10 years ago  ](https://developer.wordpress.org/reference/functions/add_rewrite_rule/#comment-1629)
 24.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_rewrite_rule%2F%23comment-1629)
      Vote results for this note: 5[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_rewrite_rule%2F%23comment-1629)
 25.  A quick example: processing rules to make a subscribers page, hoping it helps.
 26.      ```php
          add_action('init', function() {
      
          	$page_id = 2; // update 2 (sample page) to your custom page ID where you can get the subscriber(s) data later
          	$page_data = get_post( $page_id );
      
          	if( ! is_object($page_data) ) { // post not there
          		return;
          	}
      
          	add_rewrite_rule(
          		$page_data->post_name . '/subscriber/([^/]+)/?$',
          		'index.php?pagename=' . $page_data->post_name . '&my_subscribers=1&my_subscriber=$matches[1]',
          		'top'
          	);
      
          });
          ```
      
 27.  Now in your page template ( if you have one, or while filtering this custom page’s
      content ), you can get the displayed subscriber slug by calling `get_query_var('
      my_subscriber')`, but first, pass `'my_subscriber'` to the query variables:
 28.      ```php
          add_filter('query_vars', function($vars) {
              $vars[] = "my_subscriber";
              return $vars;
          });
          ```
      
 29.   * I’ll leave feedback here because you use `->post_name` column. I recommend
         that anyone who will try to use the code above, make sure to wrap `post_name`
         with `rawurldecode()` inside `regex` argument. For example: `add_rewrite_rule(
         rawurldecode($page_data->post_name), 'bla', 'top');`. In many case if your`
         post_name` or known as URL slug is in English using pattern without `rawurldecode()`
         works fine. But whenever your `post_name` is in some other language like Thai,
         the character **ค** will be **%E0%B8%84** when user type from address bar 
         but inside WordPress DB it is **%e0%b8%84**. It is different case, yes! but
         that may lead you to 404 page while you are still confuse that all the characters
         you type is correct. Wrap `post_name` with `rawurldecode()` will be fine and
         works well.
       * [vee](https://profiles.wordpress.org/okvee/) [3 years ago](https://developer.wordpress.org/reference/functions/add_rewrite_rule/#comment-6801)
 30.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_rewrite_rule%2F%3Freplytocom%3D1629%23feedback-editor-1629)
 31.   [Skip to note 15 content](https://developer.wordpress.org/reference/functions/add_rewrite_rule/?output_format=md#comment-content-4554)
 32.    [Akira Tachibana](https://profiles.wordpress.org/atachibana/)  [  6 years ago  ](https://developer.wordpress.org/reference/functions/add_rewrite_rule/#comment-4554)
 33.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_rewrite_rule%2F%23comment-4554)
      Vote results for this note: 4[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_rewrite_rule%2F%23comment-4554)
 34.  (From Codex)
       **Using Custom Templates with custom querystring**
 35.  Let’s assume you are creating a “Nutrition” page for showing nutritional information.
      This page uses a custom template and takes two variables, food and variety. Create
      a file named my-custom-template.php in your themes root directory as following:
 36.      ```php
          /**
           * Template Name: Nutritional Information
           */
          get_header(); 
      
          global $wp_query;
          echo 'Food : ' . $wp_query->query_vars['food'];
          echo '<br />';
          echo 'Variety : ' . $wp_query->query_vars['variety'];
          // ... more ...
          get_footer();
          ```
      
 37.  Using this template, create a page. In Add New of Pages screen, select “Nutritional
      Information” from Template dropdown box. You may leave blank for title or contents.
      Click Publish to publish the page. Write down the ID of created page from All
      Pages screen. Move a mouse cursor on the page title to show the link information
      in status bar of Web browser. You will see the ID after the POST= in URL. In 
      this example, assume the ID is 12.
 38.  Use [add_rewrite_tag()](https://developer.wordpress.org/reference/functions/add_rewrite_tag/)
      to make WordPress aware of custom querystring variables food and variety. Add
      the following code to the functions.php file,
 39.      ```php
          function custom_rewrite_tag() {
            add_rewrite_tag('%food%', '([^&]+)');
            add_rewrite_tag('%variety%', '([^&]+)');
          }
          add_action('init', 'custom_rewrite_tag', 10, 0);
          ```
      
 40.  You can call the page as following:
       [http://example.com/index.php?page_id=12&food=milkshake&variety=strawberry](http://example.com/index.php?page_id=12&food=milkshake&variety=strawberry)
      The page will show two values passed by querystring variables.
 41.  Now, instead of passing ugly querystring variables to the page, you can set up
      a rewrite rule to create some custom pretty URLs. Add the following rule to the
      functions.php file and replace the page ID 12 by the ID that you investigated
      in above step. Don’t forget to click Save Changes in Permalinks Settings. (refer
      above IMPORTANT)
 42.      ```php
          function custom_rewrite_rule() {
              add_rewrite_rule('^nutrition/([^/]*)/([^/]*)/?','index.php?page_id=12&food=$matches[1]&variety=$matches[2]','top');
          }
          add_action('init', 'custom_rewrite_rule', 10, 0);
          ```
      
 43.  User can access the same page with following URL:
       [http://example.com/nutrition/milkshakes/strawberry/](http://example.com/nutrition/milkshakes/strawberry/)
 44.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_rewrite_rule%2F%3Freplytocom%3D4554%23feedback-editor-4554)
 45.   [Skip to note 16 content](https://developer.wordpress.org/reference/functions/add_rewrite_rule/?output_format=md#comment-content-2465)
 46.    [iqbalbary](https://profiles.wordpress.org/iqbalbary/)  [  9 years ago  ](https://developer.wordpress.org/reference/functions/add_rewrite_rule/#comment-2465)
 47.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_rewrite_rule%2F%23comment-2465)
      Vote results for this note: 2[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_rewrite_rule%2F%23comment-2465)
 48.   Example to add rewrite rule query as optional,hope it helps.
 49.      ```php
          function custom_rewrite_rule() {
          	add_rewrite_rule('^nutrition/?([^/]*)/?','index.php?page_id=12&food=$matches[1]','top');
          }
          add_action('init', 'custom_rewrite_rule', 10, 0);
          ```
      
 50.  The main things here is to add **?** in the start of your regex. Like `?([^/]*)`
 51.  Now, you can set default value for optional query where you will use the query.
 52.      ```php
          $food = $wp_query->get( 'food' );
          if( isset($food) && !empty($food) ) {
          	$nutrition = $food;
          }else{
          	$nutrition = 'strawberry'; //default value
          }
          ```
      
 53.  Now you will get the same result for `http://example.com/nutrition/strawberry`
      or `http://example.com/nutrition/`
 54.   * I am very confused on why the /?([^/]*)/? is necessary. If you can explain
         it, that will be great. * means 0 or more, but if you change it to + (1 or
         more) it fails to match. Why have the /? when the forward slash is necessary?
         The ? means 1 or 0. I removed ? and it (url matching) failed.
       * [wtlwp](https://profiles.wordpress.org/wtlwp/) [6 years ago](https://developer.wordpress.org/reference/functions/add_rewrite_rule/#comment-4117)
 55.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_rewrite_rule%2F%3Freplytocom%3D2465%23feedback-editor-2465)
 56.   [Skip to note 17 content](https://developer.wordpress.org/reference/functions/add_rewrite_rule/?output_format=md#comment-content-4555)
 57.    [Akira Tachibana](https://profiles.wordpress.org/atachibana/)  [  6 years ago  ](https://developer.wordpress.org/reference/functions/add_rewrite_rule/#comment-4555)
 58.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_rewrite_rule%2F%23comment-4555)
      Vote results for this note: 1[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_rewrite_rule%2F%23comment-4555)
 59.  (From Codex)
       **Using rewrite rules to redirect to scripts other than index.php**
 60.  The $redirect argument works slightly differently when redirecting to a custom
      PHP script because WordPress delegates these redirects to .htaccess instead of
      processing them itself. For this reason, querystring variables should be written
      like $1 instead of $matches[1]. Given we’re redirecting to a custom PHP script,
      adding the same rewrite rule from our previous example would look like this:
 61.      ```php
          function custom_rewrite_rule() {
              add_rewrite_rule('^nutrition/([^/]*)/([^/]*)/?','url/to/my/script.php?food=$1&variety=$2','top');
          }
          add_action('init', 'custom_rewrite_rule', 10, 0);
          ```
      
 62.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_rewrite_rule%2F%3Freplytocom%3D4555%23feedback-editor-4555)
 63.   [Skip to note 18 content](https://developer.wordpress.org/reference/functions/add_rewrite_rule/?output_format=md#comment-content-4787)
 64.    [Md Aminur Islam](https://profiles.wordpress.org/aminurislam01/)  [  6 years ago  ](https://developer.wordpress.org/reference/functions/add_rewrite_rule/#comment-4787)
 65.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_rewrite_rule%2F%23comment-4787)
      Vote results for this note: 1[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_rewrite_rule%2F%23comment-4787)
 66.  Rewrite rules with optional parameters.
 67.      ```php
          // siteurl.com/wpdocs-product-category/edit-product-category/43 for edit product category
          add_rewrite_rule(
          	'^wpdocs-product-category/([^/]*)/([^/]*)/?',
          	'index.php?pagename=wpdocs-product-category&wpdocs-pro-category-action=$matches[1]&wpdocs-product-cat-id=$matches[2]',
          	'top'
          );
      
          // Product Category Add Page
          // siteurl.com/wpdocs-product-category/add-product-category for add product category
          add_rewrite_rule(
          	'^wpdocs-product-category/([^/]*)/?',
          	'index.php?pagename=wpdocs-product-category&wpdocs-pro-category-action=$matches[1]',
          	'top'
          );
          ```
      
 68.  Filters the query variables allowed before processing.
 69.      ```php
          add_filter( 'query_vars', function( $vars ) {
          	$vars[] = 'wpdocs-pro-category-action';
          	$vars[] = 'wpdocs-product-cat-id';
          	$vars[] = 'wpdocs-category-action';
      
              return $vars;
          } );
          ```
      
 70.  Retrieve query parameters values
 71.      ```php
          $pro_category_action = get_query_var( 'wpdocs-pro-category-action' );
          $product_cat_id = get_query_var( 'wpdocs-product-cat-id' );
          ```
      
 72.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_rewrite_rule%2F%3Freplytocom%3D4787%23feedback-editor-4787)
 73.   [Skip to note 19 content](https://developer.wordpress.org/reference/functions/add_rewrite_rule/?output_format=md#comment-content-4553)
 74.    [Akira Tachibana](https://profiles.wordpress.org/atachibana/)  [  6 years ago  ](https://developer.wordpress.org/reference/functions/add_rewrite_rule/#comment-4553)
 75.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_rewrite_rule%2F%23comment-4553)
      Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_rewrite_rule%2F%23comment-4553)
 76.  (From Codex)
       **Basic Usage**
 77.  You can retrieve any page by specifying ID in URL as following:
       [http://example.com/?p=95](http://example.com/?p=95)
      If you add the following rule to the functions.php file, you can provide custom
      formed URL to access.
 78.      ```php
          function custom_rewrite_basic() {
            add_rewrite_rule('^leaf/([0-9]+)/?', 'index.php?page_id=$matches[1]', 'top');
          }
          add_action('init', 'custom_rewrite_basic');
          ```
      
 79.  **NOTE**: When using $matches[] to retrieve the values of a matched URL, capture
      group data starts at 1, not 0.
 80.  **IMPORTANT**: Do not forget to flush and regenerate the rewrite rules database
      after modifying rules. From WordPress Administration Screens, Select Settings-
      > Permalinks and just click Save Changes without any changes.
 81.  Now, you can access the same page as
       [http://example.com/leaf/95](http://example.com/leaf/95)
 82.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_rewrite_rule%2F%3Freplytocom%3D4553%23feedback-editor-4553)
 83.   [Skip to note 20 content](https://developer.wordpress.org/reference/functions/add_rewrite_rule/?output_format=md#comment-content-6412)
 84.    [Andre Gagnon](https://profiles.wordpress.org/2winfactor/)  [  4 years ago  ](https://developer.wordpress.org/reference/functions/add_rewrite_rule/#comment-6412)
 85.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_rewrite_rule%2F%23comment-6412)
      Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_rewrite_rule%2F%23comment-6412)
 86.  If you are distributing this in a plugin or theme, you may want to build in flushing
      rewrite rules. You can actually check to see if it has been set before you `flush_rewrite_rules();`
      using the `rewrite_rules` option.
 87.  This option is autoloaded, so there is no extra database call.
 88.  Here’s a snippet:
 89.      ```php
          add_action( 'init',  function() {
              // get registered rewrite rules.
              $rules = get_option( 'rewrite_rules', array() );
      
              // set the regex.
              $regex = 'wpdocsparam/([a-z0-9-]+)[/]?$';
      
              // add the rewrite rule.
              add_rewrite_rule( $regex, 'index.php?wpdocsparam=$matches[1]', 'top' );
      
              // maybe flush rewrite rules if it was not previously in the option.
              if ( ! isset( $rules[ $regex ] ) ) {
                  flush_rewrite_rules();
              }
          } );
          ```
      
 90.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_rewrite_rule%2F%3Freplytocom%3D6412%23feedback-editor-6412)
 91.   [Skip to note 21 content](https://developer.wordpress.org/reference/functions/add_rewrite_rule/?output_format=md#comment-content-6864)
 92.    [Pektsekye](https://profiles.wordpress.org/pektsekye/)  [  3 years ago  ](https://developer.wordpress.org/reference/functions/add_rewrite_rule/#comment-6864)
 93.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_rewrite_rule%2F%23comment-6864)
      Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_rewrite_rule%2F%23comment-6864)
 94.  IMPORTANT NOTE:
 95.  Your code with the **[add_rewrite_rule()](https://developer.wordpress.org/reference/functions/add_rewrite_rule/)**
      function should be available for the (Settings / Permalinks) **back-end page**.
 96.  I have spend much time to find out why my rewrite rule is not registered.
       The
      problem was that my code with the [add_rewrite_rule()](https://developer.wordpress.org/reference/functions/add_rewrite_rule/)
      was available only for the front-end pages. So it could not be executed when 
      I tried to flush the permalink settings in the back-end.
 97.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_rewrite_rule%2F%3Freplytocom%3D6864%23feedback-editor-6864)
 98.   [Skip to note 22 content](https://developer.wordpress.org/reference/functions/add_rewrite_rule/?output_format=md#comment-content-6045)
 99.    [Khoi Pro](https://profiles.wordpress.org/khoipro/)  [  4 years ago  ](https://developer.wordpress.org/reference/functions/add_rewrite_rule/#comment-6045)
 100. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_rewrite_rule%2F%23comment-6045)
      Vote results for this note: -1[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_rewrite_rule%2F%23comment-6045)
 101. If you need to keep custom taxonomy and post type using same slug, there is a
      way to use add_rewrite_rule for doing that. A simple case:
 102. Example:
 103.  * /course/course-post-name (custom post type)
       * /course/course-category-name (custom term)
 104.     ```php
          function wpdocs_add_course_category_rewrites() {
          	$rules = array();
          	$tax = 'course_category';
          	$post_type = 'course';
      
          	$terms = get_terms( array(
          		'taxonomy' => $tax,
          		'hide_empty' => false,
          	) );
      
          	foreach ( $terms as $term ) {
          		add_rewrite_rule( '^course/(' . $term->slug . ')[/]?$', 'index.php?post_type=' . $post_type . '&' . $tax . '=$matches[1]', 'top' );
          	}
          }
          add_action( 'init', 'wpdocs_add_course_category_rewrites' );
          ```
      
 105. Remember to flush permalinks (Settings / Permalinks) once to apply changes.
 106.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_rewrite_rule%2F%3Freplytocom%3D6045%23feedback-editor-6045)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_rewrite_rule%2F)
before being able to contribute a note or feedback.