Wordpress rewrites not working and shows 404 page - wordpress

I want to load a page with this requests:
/foo/bar
/foo/bar/bar
But this isn't working and shows 404 page:
function custom_rewrite_rule() {
add_rewrite_rule('^foo\/','index.php?page_id=205533','top');
}
add_action('init', 'custom_rewrite_rule');

The same piece of code worked.
But the settings for the Permalinks needed to be saved again.
From WordPress Administration Screens, Select Settings -> Permalinks and just click Save Changes without any changes.

function wpd_foo_rewrite_rule() {
add_rewrite_rule(
'^foo/([^/]*)/?',
'index.php?page_id=$matches[1]&param=foo',
'top'
);
}
add_action( 'init', 'wpd_foo_rewrite_rule' );
This takes whatever comes after foo/ and sets that as pagename for the query, and then param gets the static value foo. If you need different URL patterns, you'll need extra rules for each unique pattern

Related

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: Why Isn't add_rewrite_rule Working?

Goal
A visible URL of:
/products/building-automation/details/productSKU
Should point to the real WordPress page of:
/products/details/?SKU=productSKU
Code
public function setup_frontend() {
add_rewrite_rule(
'products/(building-automation|lighting-controls)/details/([^/]*)',
'products/details/?SKU=$matches[2]',
'top'
);
// Other code logic...
}
From what I can tell, the $matches points to the portions of the regex that are in (), so in the example, $matches[1] would be building-automation, and $matches[2] would be productSKU.
I have tried the flushing the rules, many times. This is being called from inside a class, but that doesn't appear to be the issue. I'm attaching the function to the init event.
What is Happening
I am getting the 404 template. What am I doing wrong?
Edit
Turns out, the second part of the parameter HAS to be index.php. Since I was trying to point to products/..., it broke. So I've updated to this:
add_rewrite_rule(
'products/(building-automation|lighting-controls)/details/([^/]*)',
'?pagename=products/details&SKU=$matches[2]',
'top'
);
It's not working and point to the right page and template. However, the $matches part is still not coming through. In fact, $_GET['SKU'] is coming through as '$matches[2]'.
The first problem is that it's pointing to something that doesn't start with index.php. It has to point to index.php?stuff, unfortunately. But you can still use the page name, as shown below.
add_rewrite_rule(
'products/(building-automation|lighting-controls)/details/([^/]*)',
'index.php?pagename=products/details&SKU=$matches[2]',
'top'
);
Second, you need to register your query string, which is SKU. Add this:
add_filter( 'query_vars', 'product_details_rewrite_filter' );
function product_details_rewrite_filter( $query_vars ){
$query_vars[] = 'SKU';
return $query_vars;
}
You will still have a problem. $_GET['SKU'] will be $matches[2], not RIBU1C. To fix this, change $matches[2] to $2. Then it will work:
add_rewrite_rule(
'products/(building-automation|lighting-controls)/details/([^/]*)',
'index.php?pagename=products/details&SKU=$2',
'top'
);
1) Try to update permalinks.
Go to Dashboard->Setting->Permalinks-> button "Save changes"
2) Add action to your function
add_action('init', 'setup_frontend');
function setup_frontend() {
add_rewrite_rule(
'products/(building-automation|lighting-controls)/details/([^/]*)',
'products/details/?SKU=$matches[2]',
'top'
);
// Other code logic...
}
It doesn't work in this way... You trying to achive impossible with this code. Rewrites Rules in wordpress work with index.php routing, but not external pathes. So
products/(building-automation|lighting-controls)/details/([^/]*)
should be "translatable" to "wordpress" language (something like)
index.php?post_type=products&category=$matches[1]&scu=$matches[1]
notice, scu should also be defined as extra var.
edit 1:
Thing (I think) you trying to achive is really looks like a htaccess rule pattern, in this case you can use mod_rewrite_rules filter (it filter content that goes to .htaccess) to change it as you like it.
edit 2:
Advertisment - you can also use my debug bar plugin called debug bar rewrite rules to basically debug this data. Notice: (currently it doesn't support rules list via add_rewrite_rule, so you can only visually inspect rule, and see if it matches your expectation).

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

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