.htaccess: Rewite rule to change to Semantic URL Struture - wordpress

I am trying to change the URL structure of a search filter using .htaccess from this:
/tours/?tour-type=guided&tour-destination=europe&tour-type=guided&tour-duration=17%2B&tour-season=2020
to this:
/tours/guided/europe/guided/17/2020
Obviously the search filter has many different options such as days, locations different types and the like, I know it's possible in .htaccess but its proving elusive to me.

In wordpress it's better to use add_rewite_rule function. In example below I assume that tours is custom post type so post_type=tours. You can put there page id, category slug or whatever you want.
Note that with this rule if you want to send only tour-season you have to send something for rest of variables.
function tour_add_rewrite_rules() {
add_rewrite_rule('^tours/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$', 'index.php?post_type=tours&tour-type=$matches[1]&tour-destination=$matches[2]&tour-type=$matches[3]&tour-duration=$matches[4]&tour-season=$matches[5]', 'top' );
}
add_action('init', 'tour_add_rewrite_rules', 10, 0);

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

Wordpress url parameter "country" on every page

I would like to use a custom URL parameter for my wordpress site, called country.
This parameter should only be used internally.
For translation I use WPML. I know that it would be possible to create a language for every country / language combination but then I would have to maintain the same language for so many countries and that would be too much effort.
Anybody got an idea how I can make the url look something like that?
www.my-domain.com/de/at -> front page
www.my-domain.com/de/at/my-post -> other page
add_action('init', 'rewritecustom');
function rewritecustom(){
add_rewrite_tag('%country%', '([^&]+)', 'country=');
add_rewrite_tag('%lang%', '([^&]+)', 'lang=');
add_rewrite_rule(
'^([a-z]{2})\/([a-z]{2})\/([^\/]*)',
'index.php?pagename=$matches[3]&country=$matches[2]&lang=$matches[1]',
'top'
);
}
In the permalink settings I set the url to
https://www.my-domain.com/%lang%/%country%/%postname%/
But unfortunately I get a 404 error when I try this.
Anybody got an idea how to solve it?

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.

altering the htaccess file of my WordPress

I need help altering the htaccess file of my WordPress site to all me to add new variables to the end of every url.
Like i want to add /es/ to the end of my WordPress URLs
Please guide they way i can do it
i tried
custom paramlink like below but it did not worked
/%post_id%/es/
As was pointed out in the comments, you cannot accomplish this with .htaccess.
It looks like what you want to do is add an endpoint to your posts:
https://codex.wordpress.org/Rewrite_API/add_rewrite_endpoint
add_rewrite_endpoint( 'es', EP_PERMALINK | EP_PAGES );
The above code will add the es endpoint to your posts and pages. Make sure you flush your rewrite rules (by visiting the permalinks settings page) after adding that line, otherwise it won't have any affect.
You could then check if the endpoint is in use like this:
global $wp_query;
if(isset( $wp_query->query_vars['es'] )) {
...
}
If your end goal is to localize your site, I recommend you using something like WPML instead.

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