src/Rewrite_Command.php
public function structure( $args, $assoc_args ) {
		global $wp_rewrite;

		// copypasta from /wp-admin/options-permalink.php

		$blog_prefix = '';
		$prefix      = $blog_prefix;
		if ( is_multisite() && ! is_subdomain_install() && is_main_site() ) {
			$blog_prefix = '/blog';
		}

		$permalink_structure = ( 'default' === $args[0] ) ? '' : $args[0];

		if ( ! empty( $permalink_structure ) ) {
			$permalink_structure = preg_replace( '#/+#', '/', '/' . str_replace( '#', '', $permalink_structure ) );
			if ( $prefix && $blog_prefix ) {
				$permalink_structure = $prefix . preg_replace( '#^/?index\.php#', '', $permalink_structure );
			} else {
				$permalink_structure = $blog_prefix . $permalink_structure;
			}
		}
		$wp_rewrite->set_permalink_structure( $permalink_structure );

		// Update category or tag bases
		if ( isset( $assoc_args['category-base'] ) ) {

			$category_base = $assoc_args['category-base'];
			if ( ! empty( $category_base ) ) {
				$category_base = $blog_prefix . preg_replace( '#/+#', '/', '/' . str_replace( '#', '', $category_base ) );
			}
			$wp_rewrite->set_category_base( $category_base );
		}

		if ( isset( $assoc_args['tag-base'] ) ) {

			$tag_base = $assoc_args['tag-base'];
			if ( ! empty( $tag_base ) ) {
				$tag_base = $blog_prefix . preg_replace( '#/+#', '/', '/' . str_replace( '#', '', $tag_base ) );
			}
			$wp_rewrite->set_tag_base( $tag_base );
		}

		// make sure we detect mod_rewrite if configured in apache_modules in config
		self::apache_modules();

		WP_CLI::success( 'Rewrite structure set.' );

		// Launch a new process to flush rewrites because core expects flush
		// to happen after rewrites are set
		$new_assoc_args = [];
		$cmd            = 'rewrite flush';
		if ( Utils\get_flag_value( $assoc_args, 'hard' ) ) {
			$cmd                   .= ' --hard';
			$new_assoc_args['hard'] = true;
			if ( ! in_array( 'mod_rewrite', (array) WP_CLI::get_config( 'apache_modules' ), true ) ) {
				WP_CLI::warning( 'Regenerating a .htaccess file requires special configuration. See usage docs.' );
			}
		}

		$process_run = WP_CLI::runcommand( $cmd );
		if ( ! empty( $process_run->stderr ) ) {
			// Strip "Warning: "
			WP_CLI::warning( substr( $process_run->stderr, 9 ) );
		}
	}
public function structure( $args, $assoc_args ) {
		global $wp_rewrite;

		// copypasta from /wp-admin/options-permalink.php

		$blog_prefix = '';
		$prefix      = $blog_prefix;
		if ( is_multisite() && ! is_subdomain_install() && is_main_site() ) {
			$blog_prefix = '/blog';
		}

		$permalink_structure = ( 'default' === $args[0] ) ? '' : $args[0];

		if ( ! empty( $permalink_structure ) ) {
			$permalink_structure = preg_replace( '#/+#', '/', '/' . str_replace( '#', '', $permalink_structure ) );
			if ( $prefix && $blog_prefix ) {
				$permalink_structure = $prefix . preg_replace( '#^/?index\.php#', '', $permalink_structure );
			} else {
				$permalink_structure = $blog_prefix . $permalink_structure;
			}
		}
		$wp_rewrite->set_permalink_structure( $permalink_structure );

		// Update category or tag bases
		if ( isset( $assoc_args['category-base'] ) ) {

			$category_base = $assoc_args['category-base'];
			if ( ! empty( $category_base ) ) {
				$category_base = $blog_prefix . preg_replace( '#/+#', '/', '/' . str_replace( '#', '', $category_base ) );
			}
			$wp_rewrite->set_category_base( $category_base );
		}

		if ( isset( $assoc_args['tag-base'] ) ) {

			$tag_base = $assoc_args['tag-base'];
			if ( ! empty( $tag_base ) ) {
				$tag_base = $blog_prefix . preg_replace( '#/+#', '/', '/' . str_replace( '#', '', $tag_base ) );
			}
			$wp_rewrite->set_tag_base( $tag_base );
		}

		// make sure we detect mod_rewrite if configured in apache_modules in config
		self::apache_modules();

		WP_CLI::success( 'Rewrite structure set.' );

		// Launch a new process to flush rewrites because core expects flush
		// to happen after rewrites are set
		$new_assoc_args = [];
		$cmd            = 'rewrite flush';
		if ( Utils\get_flag_value( $assoc_args, 'hard' ) ) {
			$cmd                   .= ' --hard';
			$new_assoc_args['hard'] = true;
			if ( ! in_array( 'mod_rewrite', (array) WP_CLI::get_config( 'apache_modules' ), true ) ) {
				WP_CLI::warning( 'Regenerating a .htaccess file requires special configuration. See usage docs.' );
			}
		}

		$process_run = WP_CLI::runcommand( $cmd );
		if ( ! empty( $process_run->stderr ) ) {
			// Strip "Warning: "
			WP_CLI::warning( substr( $process_run->stderr, 9 ) );
		}
	}
public function structure ( $ args , $ assoc_args ) { global $ wp_rewrite ; // copypasta from /wp-admin/options-permalink.php $ blog_prefix = '' ; $ prefix = $ blog_prefix ; if ( is_multisite ( ) && ! is_subdomain_install ( ) && is_main_site ( ) ) { $ blog_prefix = '/blog' ; } $ permalink_structure = ( 'default' === $ args [ 0 ] ) ? '' : $ args [ 0 ] ; if ( ! empty ( $ permalink_structure ) ) { $ permalink_structure = preg_replace ( '#/+#' , '/' , '/' . str_replace ( '#' , '' , $ permalink_structure ) ) ; if ( $ prefix && $ blog_prefix ) { $ permalink_structure = $ prefix . preg_replace ( '#^/?index\.php#' , '' , $ permalink_structure ) ; } else { $ permalink_structure = $ blog_prefix . $ permalink_structure ; } } $ wp_rewrite -> set_permalink_structure ( $ permalink_structure ) ; // Update category or tag bases if ( isset ( $ assoc_args [ 'category-base' ] ) ) { $ category_base = $ assoc_args [ 'category-base' ] ; if ( ! empty ( $ category_base ) ) { $ category_base = $ blog_prefix . preg_replace ( '#/+#' , '/' , '/' . str_replace ( '#' , '' , $ category_base ) ) ; } $ wp_rewrite -> set_category_base ( $ category_base ) ; } if ( isset ( $ assoc_args [ 'tag-base' ] ) ) { $ tag_base = $ assoc_args [ 'tag-base' ] ; if ( ! empty ( $ tag_base ) ) { $ tag_base = $ blog_prefix . preg_replace ( '#/+#' , '/' , '/' . str_replace ( '#' , '' , $ tag_base ) ) ; } $ wp_rewrite -> set_tag_base ( $ tag_base ) ; } // make sure we detect mod_rewrite if configured in apache_modules in config self :: apache_modules ( ) ; WP_CLI :: success ( 'Rewrite structure set.' ) ; // Launch a new process to flush rewrites because core expects flush // to happen after rewrites are set $ new_assoc_args = [ ] ; $ cmd = 'rewrite flush' ; if ( Utils \ get_flag_value ( $ assoc_args , 'hard' ) ) { $ cmd .= ' --hard' ; $ new_assoc_args [ 'hard' ] = true ; if ( ! in_array ( 'mod_rewrite' , ( array ) WP_CLI :: get_config ( 'apache_modules' ) , true ) ) { WP_CLI :: warning ( 'Regenerating a .htaccess file requires special configuration. See usage docs.' ) ; } } $ process_run = WP_CLI :: runcommand ( $ cmd ) ; if ( ! empty ( $ process_run -> stderr ) ) { // Strip "Warning: " WP_CLI :: warning ( substr ( $ process_run -> stderr , 9 ) ) ; } }
Updates the permalink structure.

Sets the post permalink structure to the specified pattern.

To regenerate a .htaccess file with WP-CLI, you'll need to add
the mod_rewrite module to your [WP-CLI config](https://make.wordpress.org/cli/handbook/config/#config-files).
For example:

```
apache_modules:
- mod_rewrite
```

## OPTIONS

<permastruct>
: The new permalink structure to apply.

[--category-base=<base>]
: Set the base for category permalinks, i.e. '/category/'.

[--tag-base=<base>]
: Set the base for tag permalinks, i.e. '/tag/'.

[--hard]
: Perform a hard flush - update `.htaccess` rules as well as rewrite rules in database.

## EXAMPLES

$ wp rewrite structure '/%year%/%monthnum%/%postname%/'
Success: Rewrite structure set.
Updates the permalink structure .