I have a parent/child and another parent/child/child pages. I have 2 separate page templates for both pages and the following add_rewrite_rule() code in place:
add_rewrite_rule( '^user/dashboard/([^/]*)/?’, 'index.php?pagename=user/dashboard&profile=$matches[1]', 'top' );
add_rewrite_rule( '^user/dashboard/billing/([^/]*)/?’, 'index.php?pagename='user/dashboard/billing&profile=$matches[1]', 'top' );
The first one (i.e. parent/child) works fine but the 2nd one (i.e parent/child/child) is not working. Meaning when I go to the parent/child/child page, it is using the template of the parent/child page and not the one assigned to itself.
If I comment out the first add_rewrite_rule() line, it works as expected. Seems like the first one is overriding the 2nd one.
I have tried flushing the rewrite rules manually by going to settings > permalinks.
I have even tried to change the order of the rewriting rules i.e. I tried registering the parent/child/child path before parent/child rule and then flushed the rules manually. But that also didn't help.
Any help is appreciated.
You need to change the order, first comes the more specific add_rewrite_rule , then the general one, try this
add_rewrite_rule( '^user/dashboard/billing/([^/]*)/?', 'index.php?pagename=user/dashboard/billing&profile=$matches[1]', 'top' );
add_rewrite_rule( '^user/dashboard/([^/]*)/?', 'index.php?pagename=user/dashboard&profile=$matches[1]', 'top' );
After the change, you have to turn off/on the permalinks in the settings to clear the cache.
Related
Is anyone able to comment why this does not work.
I have created a custom post type called "knowledge-hub". I also then created a page called "knowledge-hub" to act as the landing page for my CPT listings, which I show using code. On one of the CPT pages, "minimum-wage-policies", I want to be able to access a variable in the URL, e.g. ?some_var=xyz. But I want it friendly like /knowledge-hub/minimum-wage-policies/xyz.
So I have created rewrite URLs. The page name "minimum-wage-policies" will vary for many of the CPT pages, so I cant hard code this. I want to access it using a URL part.
But I cant get the add_rewrite_rule() to match matches[1] to the page name. If I hard code this page name in the add_rewrite_rule it works! But as soon as I replace it with matches[1] it does not work, its either a 404 or a redirect back to minimum-wage-policies with no variable on the end.
URL would be:
http://my-domain.com/knowledge-hub/minimum-wage-policies/some_var
Referencing the child page name works:
add_rewrite_rule(
'knowledge-hub/([^/]*)?/([^/]*)/?$',
'index.php?post_type=knowledge-hub&pagename=minimum-wage-policies&my_var=$matches[2]',
'top'
);
matches[2] is working in this case as I can see the value passed in the URL in my code.
But using matches[1] does not work and returns a 404:
add_rewrite_rule(
'knowledge-hub/([^/]*)?/([^/]*)/?$',
'index.php?post_type=knowledge-hub&pagename=$matches[1]&my_var=$matches[2]',
'top'
);
Visiting the WP URL http://my-domain.com/index.php?post_type=knowledge-hub&pagename=minimum-wage-policies&my_var=xyz works, so that structure is OK.
I have tried various regex expressions used by various others on the internet, so not just ([^/])?. So am not sure if it is to do with the regex and I just have not found the right one. I want to match anything. Same issue with (.)
I also have this:
function add_my_vars($vars)
{
$vars[] = 'my_var';
return $vars;
}
add_filter('query_vars', 'add_my_vars');
Update:
Using $request->matched_query I could see the URLs being called.
When I use pagename=minimum-wage-policies the URL is as expected:
post_type=knowledge-hub&pagename=minimum-wage-policies&my_var=xyz
When I use matches[1] is completely changed which is quite bizarre:
knowledge-hub=minimum-wage-policies%2Fxyz&page=
I managed to get it working with this rewrite_rule but I have not found this structure online so not sure if its technically correct but it works. Note the use of knowledge-hub=$matches[1]. This was discovered from viewing the $request->matched_query I mentioned was bizarre above. Replicating it in the rewrite_rule makes it work.
add_rewrite_rule(
'knowledge-hub/([^/]*)?/([^/]*)/?$',
'index.php?knowledge-hub=$matches[1]&my_var=$matches[2]',
'top'
);
I want to do a URL rewrite in the WordPress home page
I want to change my URL http://mysite.loc/?pays=senegal to look like http://mysite.loc/senegal.
The problem is that I am on the WordPress home page, so it will be confused with the URL of another page like http://transfert.loc/page-example.
I have already tried several optins but am completely blocked.
Here is my code example:
public function rewrite_urls(){
add_rewrite_tag( '%pays%','([^&]+)' );
add_rewrite_rule(
'([^/]+)',
'index.php?pays=$matches[1]',
'top'
);
}
Can someone help me please!
Thanks
Two problems I see- rewrite rules need to set query vars that will result in a successful main query. Setting just a custom var like slide doesn't parse to anything WordPress can load. Additionally, slide needs to be added to the recognized query vars for it to get parsed within a rule.
So, what would a rule look like that would load the front page posts in the main query? That's a good question- the posts page is a special case, the absence of any other query vars. I haven't found a way to do that with a rule, though it may exist.
An easier way to do this is with a rewrite endpoint:
function wpd_endpoint(){
add_rewrite_endpoint( 'page-example', EP_ROOT );
}
add_action( 'init', 'wpd_endpoint' );
Keep in mind that if you have code accessing values via $_GET, this still won't work, because WordPress doesn't put query vars there when rules are parsed. You can change the code to use get_query_var, or just assign it before the code tries to access it:
$_GET['page-example'] = get_query_var('page-example');
I need your help regarding WordPress URL rewrite. :o) At first, I am get this working:
add_rewrite_rule( 'category/([^/]+)/([^/]+)/?(?:page/?([0-9]{1,})?/?)?$' , 'index.php?category_name=$matches[1]&tag=$matches[2]&paged=$matches[3]', 'top');
This does some actions:
I can enter "blog.domain/category/category-name/" and get to the
category archive - with or without "page(d)"
I can also enter
"blog.domain/category/category-name/a-tag/" (or with "page/n/") and
get to the category archive, where the posts are filtered by an tag.
Now, I could extend this rewrite rule, f.e.:
add_rewrite_rule( 'category/([^/]+)/([^/]+)/([^/]+)/?(?:page/?([0-9]{1,})?/?)?$' , 'index.php?category_name=$matches[1]&tag=$matches[2]+$matches[3]&paged=$matches[4]', 'top');
This one will also works with "blog.domain/category/category-name/tag1/tag2/".
What I want to do is an pretty URL, so that users could filter the posts more and more by tags (f.e. /category/cars/racing/red/). Problem: for every new "path" (for category/cars/racing/ and category/cars/racing/red/) I need one add_rewrite_rule.
Is there any solution to reach this behavior with one single line? Maybe something like "there could be another path (as tag), but it mustn't"?
II try to remove 'category' from the permalink (Category Base name to . and link to %category%/%postname%)
But after that the page domain.com/news doesn't work (I see the template of index.php)
before it domain.com/category/news worked
domain.com/news/name-news works in both cases
why I got the error and how can I fix it?
I have category.php where I have code for content of domain.com/category/news for example
You would need to make sure that you have flushed the rewrite rules. You can do this either by going to settings->permalinks, and saving, or by putting in the following into an action. Don't keep running it on each load though, as it has performance issues
flush_rewrite_rules();
https://codex.wordpress.org/Function_Reference/flush_rewrite_rules
If that doesn't work, please show how you are setting the permalink
I tried different tutorials, answers on stack etc. - but can't seem to make this work on my particular case.
I'm trying to use Wordpress' Rewrite Rules to rewrite an ugly URL containing a variable, making it look nice and SEO-friendly. But whatever I do, it doesn't seem to apply. Tried printing $wp_rewrite->rules which returns my custom rule at the top, so I guess the system works as it should, however URL isn't being rewritten.
Here's the function in functions.php:
function create_new_url_querystring()
{
add_rewrite_tag('%hovedgruppe%', '([^&]+)');
add_rewrite_rule(
'(.+)/?$',
'index.php?page_id=75&hovedgruppe=$matches[1]',
'top'
);
}
add_action('init', 'create_new_url_querystring');
The URL I'm testing this on is www.domain.com/produkter/gruppe/?hovedgruppe=value (which means /produkter/gruppe is a page with ID 75). The ideal result would be www.domain.com/value
Tried different variations of the rewrite rule. And yes I'm flushing the rewrite rules when making changes (entering the permalink page in wp-admin). Anyone able to understand how the rewrite rule should be written to make it work?
Never mind! It actually works as it is, I just didn't realize it was working, because I was expecting the URLs to automatically change from non-rewritten to rewritten. It doesn't, but if I enter a rewritten URL, it works. :D