Wordpress automatically created a redirect that I need removed - wordpress

I made an edit to a page on a Wordpress site that included changing its parent. Unfortunately I was not paying attention and this move somehow changed the permalink/slug which I saved before noticing. In addition Wordpress automatically created a 301 redirect to go from the original URL to this new URL. I went back in and changed the permalink/slug back to the original but that redirect still stands so it stills sends the page to the new url it created rather than keeping it to updated option (updated meaning me typing in the original slug).
I am looking for a way to remove the 301 redirect, but do not know where to start.
The original URL: https://www.synergex.com/rev11
The newly generated URL: https://www.synergex.com/products/synergy-de-rev11-licensing-faq
I also tried simply creating a brand new page using the original slug/permalink /rev11 but it gets changed to /rev11-2 so it seems like it it must remember the /rev11 somewhere.
I am hoping there is a way to do this through the WP Dashboard and am open to suggestions. This site does have the Redirection plugin installed but I've checked through the lists and this redirect was not created or listed in it. I also tried to add a redirect to do the opposite (redirect from the new URL to old) but of course it just created a loop that timed out.

EDIT 1.2: You probably have a problem with your old permalinks not getting flushed properly. But do not worry the great people at Wordpress got a function for that called flush_rewrite_rules();
Remove rewrite rules and then recreate rewrite rules. This function is
useful when used with custom post types as it allows for automatic
flushing of the WordPress rewrite rules (usually needs to be done
manually for new custom post types). However, this is an expensive
operation so it should only be used when necessary.
Source # https://developer.wordpress.org/reference/functions/flush_rewrite_rules/
Add the following to the top of your function.php:
<?php
/**
* Removes a function from a specified filter hook.
* More about flush_rewrite_rules(); # https://developer.wordpress.org/reference/functions/flush_rewrite_rules/
*/
flush_rewrite_rules();
/*
* Remove rewrite rules and then recreate rewrite rules.
* More about remove_filter(); # https://developer.wordpress.org/reference/functions/remove_filter/
*/
remove_filter( 'template_redirect', 'redirect_canonical' ); ?>

Related

Home page url rewriting Wordpress

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

Appending query parameter to Woocommerce category URL

I'm trying to force display the list view in my Woocommerce store but I can't seem to get it working.
The theme has support for list view and you can force it by appending "?product_view=list" so a category URL becomes:
http://subliminalscience.com/product-category/icbch-hypnosis-certification/?product_view=list
Instead of the default one:
http://subliminalscience.com/product-category/icbch-hypnosis-certification/
I added this Rewrite Rule to my htaccess but it doesnt:
RewriteRule ^product-category(.*)$ http://subliminalscience.com/product-category$1?product_view=list
It seems Wordpress ignores this Rewrite rule. Any ideas?
I'm surprised this answer stands as the only one.
Making changes in .htaccess to force this sort of behaviour seems really unnecessary and obviously isn't useful for others trying to solve this issue, especially those users on nginx servers or on shared hosting without .htaccess access.
You should try to fix this in PHP using an action.
Looking at your question, I can tell that theme you have is checking for GET data in the URL bar, which is the ?key1=value1&key2=value2 part of an URL. GET is an HTTP method that you can read about here if you want to learn more.
You can actually set GET data in PHP, and you can safely put this into your functions.php file.
You will want to create a function that simply checks the current page, and if it's a product category page sets the GET data.
At the bottom of your functions.php, you'd want to add something like this:
<?php
//Force all category pages to list view.
add_action('woocommerce_before_main_content','force_category_list_view', 5);
function force_category_list_view(){
if(is_product_category()){
$_GET['product_view'] = 'list';
}
};
?>
I actually can't test this, but I think it'd work. Essentially, in the woocommerce template archive-product.php, the first action that runs is 'woocommerce_before_main_content'. What we're doing is calling our function, which checks the page is indeed a product category. If it is, it sets the GET variable as list, which is exactly what the URL is telling the page to do already.
Mainly this is just a better practise than altering your .htaccess, but someone determined could also override that GET data by changing the URL to read ?product_view=list&product_view=xyz I can't imagine this would be an issue in this instance, but in other instances it might be.

How to update update existing custom post type with register_post_type permanently?

I want to perform quick rewrite slug update for my custom post type on the fly. So I use code like in my theme's functions.php:
$no = get_post_type_object($pt_slug);
$no -> rewrite['with_front'] = false;
$no -> rewrite['slug'] = $slug;
register_post_type($pt_slug, $no );
Which is hooked to 'init' add_action('init', 'check_post_type_rewrite_url');
There is large code so I write only issue part in here.
register_post_type returns object with updated slug. I understand it means my custom post type was updated, but it was not. Existing post type still has its old rewrite slug. Should I add something special for rewrite rules to save changes and make it work? Or there is specific way to register/update post types with rewrite slug so it worked?
The way you solved the issue - by using flush_rewrite_rules - is technically correct but not recommended. As stated in the doc:
This function is useful when used with custom post types as it allows for automatic flushing of the WordPress rewrite rules (usually needs to be done manually for new custom post types). However, this is an expensive operation so it should only be used when absolutely necessary.
So you shouldn't keep your flush_rewrite_rules() call in your init hook, as it will regenerate all the rewrite rules at every page load, which is really badly ressource consuming.
Usually it's enough to just visit the Permalinks Settings page to flush the rules - so if you change again your CPT slug in the future, just visit the page once.
If your slug could change dynamically, then you can make use of flush_rewrite_rules(), but you need to use it carefully - it shouldn't be called on every page load, but could be used by a periodic cron job, or on plugin activation / deactivation, depends of the case.

Wordpress re write rule

Lets say I have a URI thats portfolio_categories/guttersspouting/
How could I rewrite a function to change it too "products"
ok as requested this matches the uri "product" exactly. This is not advisable if releasing code to a third party, as if they create a page called product, it will follow this rule.
Btw I am assuming you mean post_type = page (will also work for posts, see the post_id= in the add_rewrite_rule? You need to change this to the post id of the page you want to redirect to.
add_action('init', 'new_rewrite_rule');
function new_rewrite_rule(){
// matches product exactly, product/aproduct will fail, etc
// you need to add your own post_id into the function below.
add_rewrite_rule('^product$','index.php?page_id=12','top');
}
You will need to flush the rewrite rules for this to work (go to the permalinks setting page and just hit the save button)
code version of flush rewrite rules, not a good idea to have it running on wp_loaded, should be on a activation hook really but for testing this will do:
function new_flush_rewrite_rules() {
flush_rewrite_rules();
}
add_action( 'wp_loaded', 'new_flush_rewrite_rules' );
Just to note you can also do this manually in the post edit screen.

Can I edit my .htaccess to write some WorldPress URL's (custom rewrites)?

So here's the problem: We don't like the fact that WordPress doesn't allow duplicate slugs, even for sub categories meaning we cannot have urls like:
product-1/guides
product-1/articles
product-2/guides
product-2/articles
That's very annoying! One solution we are considering is setting up our slugs like this:
product-1/product-1-guides
product-1/product-1-articles
product-2/product-2-guides
product-2/product-2-articles
But in our htaccess - can we use it to pick up such urls and rewrite them as prettier urls which have the product name removed from the sub folder? We don't mind hard coding these as we'll only ever have 5-10 products on the site.
This would keep the WordPress install happy with unique slugs, but the SEO tick in the box with better looking urls.
I just need a hand with the syntax please?
EDIT 1:
After looking at the WordPress Rewrite API, I'm failing to get anywhere with what I think is a really simple test. I have the following code in my functions.php which is running as I tested an echo, but no rewriting is taking place?
add_action( 'init', 'productRewrites' );
function productRewrites() {
add_rewrite_rule('^wordpress/james?','index.php?author_name=jwilson','top');
}
Nothing happens when I hit:
mysite.com/wordpress/james
Edit 2:
Cool I realise I now have to click save each time. The problem I now have is the following does not work not when I use $matches[1] - it only works if I hard code the author_name value (to jwilson for example):
function productRewrites() {
add_rewrite_rule(
"writer/([^/]+)/?",
"index.php?author_name=$matches[1]",
"top");
}
When I use $matches[1] it just returns everything! So clearly isn't using ([^/]+) in the url?!
you have to reset permalink structure
in order to do that, move to Settings -> Permalinks and press Save changes button

Resources