BOUNTY EDIT :
I have a plugin which does have links similar to below:
mydomain.com/wp-content/plugins/myplugin/includes/categories.php?c=9
I want these links to be more SEO friendly and to work like this:
mydomain.com/wp-content/plugins/myplugin/includes/categories/9/
SOLUTIONS I HAVE TRIED AND FAILED:
The plugin uses ajax. I tried using add_rewrite_rule
as below
My plugin has following function:
add_action( 'init', 'add_alexes_rules' );
function add_alexes_rules() {
add_rewrite_rule('^categories/([0-9]+)/?$', 'categories.php?c=$1', 'top');
flush_rewrite_rules();
}
and learned that it won't work after a reply from @Milo:
Internal rules should point to index.php
and tried to edit .htaccess
directly but stuck after a reply from @Deadooshka:
WP uses php-server-variables to handle request thru the index.php, so rule must change that variable.
So what is the way to achieve this?
EDIT: Jack Johansson's SOLUTION
Created a php file: page-custom.php
as below:
<?php
/**
* Template Name: My Custom Template
*/
if (is_page_template( 'page-custom.php' ) && isset($_REQUEST['cat'])){
...
}
?>
Created a page with ID: 281
which uses page-custom.php
template.
My functions.php
:
function my_rewrite_tag() {
add_rewrite_tag('%cat%', '([^&]+)');
}
function my_rewrite_rule() {
add_rewrite_rule('^categories/([^/]*)/?','index.php?page_id=281&cat=$matches[1]','top');
}
add_action('init', 'my_rewrite_tag', 10, 0);
add_action('init', 'my_rewrite_rule', 10, 0);
Now when I navigate to an example url like the one below
Problem is $_REQUEST['cat']
is empty so it never enters to the condition inside page-custom.php