在这里,请注意代码及其注释:
function my_custom_rewrite_for_react() {
// Add to your resource \'index.php\' a param and its value. Here I chose as an example \'custom_template\' and the value \'1\'.
add_rewrite_rule( \'^login/?\', \'index.php?custom_template=1\', \'top\' );
add_rewrite_rule( \'^/logout/?\', \'index.php?custom_template=1\', \'top\' );
// WordPress does not know your custom param, so tell it.
add_rewrite_tag( \'%custom_template%\', \'([^&]+)\' );
/**
* Filters the path of the current template before including it.
*
* @link https://developer.wordpress.org/reference/hooks/template_include/
*/
add_filter( \'template_include\',
/**
* @param string The path of the template to include.
*/
function ( $template ) {
global $wp_query;
// In this example \'chosen_template\' equals to \'1\'.
if ( ! empty( $wp_query->query_vars[\'custom_template\'] ) ) {
// $path will be equal to something like /var/www/my-app/public/wp-content/my-theme/index.php
$path = STYLESHEETPATH . \'/index.php\';
if ( file_exists( $path ) ) {
$template = $path;
}
}
return $template;
}
);
}
add_action( \'init\', \'my_custom_rewrite_for_react\' );
// Delete this function once everything is working fine!
// Using the function bellow is the same as visiting the Permalinks page in admin.
flush_rewrite_rules();
顺便说一句,您的代码实际上可以工作,但它会将状态为301的您重定向到
http://yoursite.com/index.php将您重定向到
http://yoursite.com,因为wordpress就是这样工作的。
要真正注意到重定向,可以打开终端(Unix)并键入curl -v http://myurl.com/login
或curl -v http://myurl.com/index.php
.