Incorrect routing with wordpress rewrite - wordpress

I have a page setup with the permalink of mydomain.com/events which uses a page template. This page show all my events pulled in from an external source (so can't be a custom post type). I've then setup rewrites to handle a categories parameter to the url and then single events.
add_action('init', 'mydomain_events_rewrite');
function mydomain_events_rewrite()
{
add_rewrite_rule(
'^events/categories/?$',
'index.php?category=$matches[1]',
'top'
);
add_rewrite_rule(
'^events/?$',
'index.php?event=$matches[1]',
'top'
);
}
add_filter('query_vars', 'mydomain_events_rewrite_var');
function mydomain_events_rewrite_var($vars)
{
$vars[] = 'events';
$vars[] = 'categories';
return $vars;
}
So the idea is categories would just provide the category variable to the events page, for example mydomain.com/events/categories/film. And then individual events would be mydomain.com/events/123/my-epic-film.
As it stands, if I go to mydomain.com/events it just redirects me to the homepage. But if I use the rewrite urls, mydomain.com/events/categories/film it goes to a 404. Where am I going wrong here?
Thanks!

So I was missing the correct regex and the page
add_rewrite_rule(
'^events/category/([^/]*)/?',
'index.php?pagename=events&category=$matches[1]',
'top'
);
add_rewrite_rule(
'^events/([^/]*)?',
'index.php?pagename=events&event=$matches[1]',
'top'
);

Related

add_rewrite_rule not working with custom post type

I am trying to follow the examples in the wordpress documentation about add_rewrite_rule and add_rewrite_tag.
I'm using a custom post type called "panel".
so the pages look like "site.com/panel/post-slug"
url queries work: for example I have 2:
var1 and testquery.
if I go to "/panel/test-page-auto/?var1=10"
And with var1 and testquery:
so what i want is, change the /panel/test-page-auto/?var1=10&testquery=Hi
-> /panel/test-page-auto/10/Hello
my code:
add_action('init', '__rewrite_tag_rule', 10, 0);
function __rewrite_tag_rule() {
$page_type = "^panel/";
add_rewrite_tag('%testquery%', '([^&]+)');
add_rewrite_tag('%var1%', '([^&]+)');
//Add rule
//panel/test-page-auto/10/edit
add_rewrite_rule(
$page_type."test-page-auto/([^/]*)/([^/]*)/?",
'index.php?pagename=test-page-auto&var1=$matches[1]&testquery=$matches[2]',
'top' );
//add rule for show
//panel/test-page-auto/10
add_rewrite_rule(
$page_type."test-page-auto/([^/]*)/?",
'index.php?pagename=test-page-auto&var1=$matches[1]&testquery=Hi',
'top' );
}
add_filter('query_vars', function($vars) {
$vars[] = "testquery";
$vars[] = "var1";
return $vars;
});
But it's not working.
when I try to put /panel/test-page-auto/10/Hi it just redirects me to /panel/test-page-auto and the queries are blank.
Can someone help me understand what I'm doing wrong?
I have already tried:
save permalinks after each change.
edit the rules in various ways.
test the links using the queries and they are working.

Why isn't my WordPress rewrite rule working?

I'm using bbPress, and bbPress uses the rewrite so that:
index.php?bbp_user={id}
becomes
/forums/users/{login}
I want to change/add it so that it's:
/forums/users/{id}
The reason is that some users' login is their email, so we don't want that in the URL of the profile page.
I have the code to change the links working:
function bbp_custom_author_link( $url, $user_id, $user_nicename){
$url = site_url()."/forums/users/".$user_id;
return $url;
}
add_filter( 'bbp_get_user_profile_url', 'bbp_custom_author_link', 10, 3);
But I keep getting 404 errors when you click the link. I'm trying to add a rewrite rule to fix it, but it's not working:
function bbp_custom_author_rewrite(){
add_rewrite_rule(
"^forums/users/([^/]*)$",
"index.php?bbp_user=$matches[1]",
"top"
);
}
add_action( 'init', 'bbp_custom_author_rewrite', 10);
I'm still getting 404 errors. I tried adding flush_rewrite_rules(); and re-saving permalinks, but it didn't change.

Wordpress multiple add_query_vars returns 404

Im using Wordpress 5.8, which is the latest version. Now im trying to make a pretty URL like https://example.com/jobs/1234/accountant.
Values in jobid and jobtitle are dynamic.
Here is my function;
add_action('init', function() {
add_rewrite_rule( 'jobs/([a-z0-9-]+)[/]?$', 'index.php?pagename=jobs&jobid=$matches[1]&jobtitle=$matches[2]', 'top' );
} );
add_filter('query_vars', function($vars) {
$vars[] = "jobid";
$vars[] = "jobtitle";
return $vars;
});
The above code only works for https://example.com/jobs/1234/
If I add another value next to jobid (1234) like (accountant) it returns to 404 page not found.
Is there a way to make it work? I dont want the url to be like https://example.com/jobs/1234/?jobtitle=accountant
I end up changing the regex and I don't get 404 now.
add_rewrite_rule( '^jobs/([^/]*)/?', 'index.php?pagename=jobs&jobid=$matches[1]&jobtitle=$matches[2]', 'top' );
To get the second value I extracted the url $wp->request().

Wordpress rewrite url not working

I 've create the page tag with page id 3055 and slug videos-tag. I want to add another segment to the url for queries like http://www.example.com/videos-tag/test so i added a rewrite rule.
add_action('init', 'custom_rewrite_tag', 10, 0);
function custom_rewrite_tag() {
add_rewrite_tag('%videos-tag%', '([^&]+)');
add_rewrite_rule('^videos-tag/([^/]*)/?','index.php?page_id=3055&q=$matches[1]','top');
}
When i click on this link http://www.example.com/videos-tag/football. I get 404 error page not found?
How do i solve?
Did you already add query_vars on your function ? If so you can change your rewrite function become this code bellow
in this case I see your query parameter is 'q', try to something more unique ie : vidq or something else.
add_action( 'init', 'wpse12065_init' );
function wpse12065_init() {
add_rewrite_rule(
'videos-tag(/([^/]+))?(/([^/]+))?/?',
'index.php?pagename=videos-tag&q=$matches[2]',
'top'
);
}
PS : Don't forget to save permalink
reference URL = http://www.rlmseo.com/blog/passing-get-query-string-parameters-in-wordpress-url/

Wordpress custom rewrite rules not working

I want to add a simple custom Wordpress rewrite rule but somehow I don't get this working.
This URL
http://www.gewerbesteuer.de/steuer/muenchen
should call this
http://www.gewerbesteuer.de/index.php?pagename=gewerbesteuerhebesaetze&loc=muenchen.
So I want to call a page, which displays tax rates for a certain city, the city should be in the url as the last part.
Here is my code:
function custom_rewrite_tag() {
global $wp_rewrite;
add_rewrite_tag('%loc%', '([^&]+)');
add_rewrite_rule('steuer/([^/]+)',
'index.php?pagename=gewerbesteuerhebesaetze&loc=$matches[1]', 'top');
flush_rewrite_rules();
}
function query_vars($query_vars ) {
$query_vars[] = 'loc';
return $query_vars;
}
add_action('init', 'custom_rewrite_tag');
add_filter( 'query_vars', 'query_vars' );
The rewrite rule is working but the parameter (in this case loc) is not picked up. Even if I hardcode the rewrite rule with a certain city like
add_rewrite_rule('steuer/([^/]+)',
'index.php?pagename=gewerbesteuerhebesaetze&loc=muenchen', 'top');
it still doesen't pick up the value of the loc parameter. I also noticed that the $matches array is empty and doesn't contain any values.
I went through all the docs at wordpress and the questions here, but couldn't find the problem. Any ideas?
Thanks
Bernhard
Can you try adding this and see if this makes any difference?
Edited
I have changed the paramter in the rewrite rule from "pagename" to "page_id" - I am assuming its 2207 as thats what it said from looking at the body class on your website's page.
I also removed flush_rewrite_rules() from the custom_rewrite_rules_tags function, can you try this now then go to Options > Permalinks and re-save again.
function custom_rewrite_rules_tags() {
global $wp_rewrite;
add_rewrite_tag('%loc%', '([^&]+)');
add_rewrite_rule(
'steuer/([^/]+)',
'index.php?page_id=2207&loc=$matches[1]',
'top'
);
}
add_action('init', 'custom_rewrite_rules_tags');
function custom_query_vars($query_vars ) {
$query_vars[] = 'loc';
return $query_vars;
}
add_filter( 'query_vars', 'custom_query_vars' );

Resources