Wordpress rewrite url not working - wordpress

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/

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.

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 Rewriting Rules

I have a Wordpress Multisite installation and I add custom post type named course in blog 1 and then I select the other blogs where I want see it.
In other blogs add a page that I use like single course template.
The url of this page is
http://example.com/blogName/course/?course-name=lorem-ipsum&course-id=xx
I would like to have a url like this
http://example.com/blogName/course/lorem-ipsum
and i would like to get the course-id parameter in template page.
I tried to use add_rewrite_rule but i'm not able to do what i want.
add_filter('query_vars', 'my_query_vars', 10, 1);
function my_query_vars($vars) {
$vars[] = 'course-name';
return $vars;
}
add_action( 'init', 'init_custom_rewrite' );
function init_custom_rewrite() {
add_rewrite_rule(
'^course/([^/]*)/?','index.php?course-name=$matches[1]','top');
}
How can I do this?
I need to add something to .htaccess?
Once you've executed the above hooks, find a way to call the flush rewrite function which will update the rewrite cache so it should start working.
flush_rewrite_rules( true );
Documentation for this function can be found on the developer docs site.
You can indirectly call that function too by just saving the Permalinks settings in the dashboard by going to Settings > Permalinks.

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

rewrite rule wordpress not working

This quetsion has been ask al lot, but however I tried I get always a 404 page in WP
I have a templatepage (wp-content/themes/responsivewizzard.php cals: wizzard. ->
I Added a page in the admin sector. The name of the page = "tespage". The template I choose is "wizzard"
When in try example.com/wp/testpage -> I get my wizzard page. No worries.. but..
I like to add a subpage to my wizzardpage. For example:
example.com/wp/testpage/nice
When i try this I get a 404 page from WP
I added this code to my function.php file. This file is in wp-content/themes/responsive directory -> my default theme
function members_rewrite_rules()
{
add_rewrite_rule('testpage /(.+)/?$', 'index.php?pagename=testpage &subpage=$matches[1]', 'top');
}
add_action( 'init', 'members_rewrite_rules' );
function members_query_vars($query_vars)
{
$query_vars[] = 'subpage';
return $query_vars;
}
add_filter('query_vars', 'members_query_vars');
Please help me
I found out the soulution. You need to reset the permalinks in the setting menu..

Resources