Why isn't my WordPress rewrite rule working? - wordpress

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.

Related

Incorrect routing with wordpress rewrite

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'
);

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/

Rewrite rule for permalink on wordpress

I am working on a wordpress site that is running in a subdomain.
I have added the subfolder on WP .htaccess file to let all the site and permalinks work, so my WP site is running under
http://example.com/subfolder/
Now I need to do the following:
I have this permalink structure that goes to a post:
example.com/subfolder/the-post-permalink
And I need to add a rule that send this URL:
example.com/subfolder/the-post-permalink-category
to:
example.com/subfolder/the-post-permalink?param=category
So the post page displayed will be the same, but I will get a GET parameter with "param" value that was sent in the rewrite rule.
I have tried this with the add_rewrite_rule() method on the functions.php file, but could not make it work. Tried adding or not the subdolfer in the rule.
add_action('init', 'add_htaccess_redirects' ,10, 0);
function add_htaccess_redirects()
{
add_rewrite_rule(
'^subfolder/the-post-permalink-(.*)',
'subfolder/the-post-permalink?param=$matches[1]',
'top'
);
}
and
add_action('init', 'add_htaccess_redirects' ,10, 0);
function add_htaccess_redirects()
{
add_rewrite_rule(
'^the-post-permalink-(.*)',
'the-post-permalink?param=$matches[1]',
'top'
);
}
Any help please?
Thanks.

Wordpress add_rewrite_rule gives 404

I want to change the permalink structure for specific custom post type.i.e
http://test.com/brand/calvin-klien?mtype=tab2
^this is dynamic
To
http://test.com/brand/calvin-klien/mtype/tab2
^this is dynamic
Here is a piece of code I tried.
Registering add_rewrite_tag
function custom_rewrite_tag() {
add_rewrite_tag('%mtype%', '([a-z0-9\-]+)');
}
add_action('init', 'custom_rewrite_tag', 10, 0);
add_action('init', 'wpse50530_journal_archive_rewrite', 10, 0);
Code1
function wpse50530_journal_archive_rewrite(){
add_rewrite_rule('brand/([a-z0-9\-]+)/([a-z0-9\-]+)/$','index.php?name=$matches[1]/?mtype=$matches[2]','top');
}
Code2
add_action('generate_rewrite_rules', 'work_list');
function work_list($wp_rewrite) {
$newrules = array();
$newrules['brand/([a-z0-9\-]+)/([a-z0-9\-]+)/$'] = 'index.php?name=$matches[1]&mtype=$matches[2]';
$wp_rewrite->rules = $newrules + $wp_rewrite->rules;
I have tried both above codes, flushed permalinks but still a 404. I dont know why it is creating $matches in htaccess as htacces doesnt know WHAT IS $matches
Also I have tried monkeyman-rewrite-analyzer plugin which is showing the correct matched result for my permalink but still word press showing 404. See attached screenshots for Code1 & Code2
The following code should help
add_action( 'init', 'so_27487849' );
function so_27487849() {
add_rewrite_rule(
'^brand/([^/]*)/mtype/([^/]*)/?',
'index.php?name=$matches[1]&mtype=$matches[2]',
'top');
}
Flush your permalinks and it should work.
I will continue Anand's code for some further changes. It was redirecting on name=$matches[1] and I need to stay at the same URL I hit, for this it must include the custom post type name.
add_action( 'init', 'so_27487849' );
function so_27487849() {
add_rewrite_rule('^brand/([^/]*)/([^/]*)/?','index.php?brand=$matches[1]&mtype=$matches[2]','top');
^--this
}
I got the Pretty URL... Yaaay!!!
BUT URL doesn't contain query string(i.e: mtype=tab1) and the rest of my code is useless, so we can achieve this by doing get_query_var('mtype') and I got the same value which was working in $_REQUEST['mtype'] and my code worked like a charm.
Also I deactivated the monkeyman pluggin
Just in case anyone has the same problem and checks out this page.
In the permalink settings page, check how the permalink structure is configured. The most common is to enter /%postname%/ in the custom structure field to let the rewrite_rules work properly.
before or after the call the function add_rewrite.. insert this code status_header(200);

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