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

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

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.

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.

Custom Plugin for wordpress with hierarchy of SEF pages

Here's my issue. My company needs a vendor database added to our wordpress website. None of the existing plugins will even come close to what we need, and we already have a mysql database with all of our information, so we need to create a plugin or something to do what we need.
These urls need to be direct-accessible and have SEF urls. So, for example:
mysite.com/vendors/
mysite.com/vendors/pipe-manufacturers/
mysite.com/vendor/bobs-pipes/
And, the custom content needs to appear inside the wordpress template.
There are really 2 options:
1) Find a way to write our application outside of wordpress, but find a way to bootstrap wordpress to show the header, footer, and sidebar.
2) Run the app from inside wordpress.
So I went for option #2. I created a new template file named "vendor.php", and began working. I added this code to my functions.php of my theme:
add_filter( 'template_include', 'xyz_template_check' );
function xyz_template_check() {
global $template;
$rqst = $_SERVER['REQUEST_URI'];
$ra = split("/", $rqst);
if ($ra[1] == "vendors") {
$template_file = get_stylesheet_directory() . '/vendors.php';
return $template_file;
}
return $template;
}
So what the above code does, if it sees the word "vendors" as the first part of the url after the site name, it sends you to vendor.php. This works PERFECTLY....
except...
Wordpress believes that the page is not found. It returns a 404 header, and NOT FOUND into the page title and breadcrumb.
Adding a PAGE called "Vendor Database" with the permalink "/vendors/" fixes the main page. But there will be literally hundreds of vendors and different categories. I cant be creating a custom page for each one. This needs to be dynamic.
So, how do I make wordpress give a 200, and supply an acceptable page title, breadcrumb, etc.
Don't even get me started on the danged wp_title filter. This did NOT work as documented. Although, it just occurred to me that this might be an issue with Wordpress SEO (the wp_title filter issue).
Anyone got an idea on this?
Ok got this. The solution was to use the rewrite api, as mentioned above, to look for the pattern /vendors/, letting it know that it was a valid URL. Coupled with my existing template override, this is what I needed.

How do I prevent Wordpress from stripping the "at" sign (#) from the URL query string?

I am trying to pass an email address to a wordpress page like so:
http://www.website.com/?email=fakeemail#yeahwho.com
However, Wordpress turns it into this:
http://www.website.com/?email=fakeemailyeahwho.com
I even try URL encoding it like so:
http://www.website.com/?email=fakeemail%40yeahwho.com
But Wordpress is too smart and still removes the %40.
I understand that # is a reserved character, but I should be able to still use the URL encoded version. Alas, Wordpress does not want it to be so.
How can I force Wordpress to respect the # sign? I'm guessing I'll either have to hack the internals, or do some mod_rewrite magic.
from http://www.webopius.com/content/137/using-custom-url-parameters-in-wordpress
First, add this to your theme's functions.php file (or make a custom plugin to do it):
add_filter('query_vars', 'parameter_queryvars' );
function parameter_queryvars( $qvars )
{
$qvars[] = 'email';
return $qvars;
}
Next, try passing ?email=fakeemail-AT-yeahwho.com in the URL and then converting it back with something like this:
global $wp_query;
if (isset($wp_query->query_vars['email']))
{
$getemail = str_replace( '-AT-', '#', $wp_query->query_vars['email']);
}
// now use $getemail
This would only not work in the very rare occurrence of an email that actually has "-at-" in it. You could replace for an even more obscure string like '-AT6574892654738-' if you are concerned about this.
Whatever your final solution, don't hack the core to get it to work. :)
I was having a similar problem and I was able to isolate the issue to an SEO plugin. I'm sure the plugin added a filter to the functions.php but as the plugin wasn't being used uninstalling the plugin also resolved the issue.
I also had this problem, but it wasn't caused by a plugin. It was a result of the 301 redirect that WordPress does with regard to your Site URL having, or not having, a www. in it.
If my Site URL was defined as http://www.mydomain.com, then this would work as expected: http://www.mydomain.com/?email=user#domain.com
If the user came to the site as: http://mydomain.com/?email=user#domain.com (NOTE: no www), then WordPress would redirect to this: http://www.mydomain.com/?email=userdomain.com (NOTE: the stripped # symbol)
My solution was to hard code the www redirect in the htaccess file, so WordPress would never have the opportunity to mess with my URL. This page gives example htaccess lines to redirect non www to www and vice versa: http://dense13.com/blog/2008/02/27/redirecting-non-www-to-www-with-htaccess/
I was having a similar problem today when trying to pass Mailchimp data through to a Gravity Form in Wordpress. I found a solution. The original question stated that Wordpress was also stripping %40, but it didn't for me in this instance.
1) In Mailchimp create a new Merge tag. I called mine 'Email Param' and * |EMAIL2| *
2) Export your list of subscribers
3) Copy the normal 'email' column content into the new 'Email Param' column.
4) Do a Find and Replace for all # symbols to %40
5) Import your list and tick the box that Auto-updates that list
6) Update your URL to include the new parameter
* |EMAIL2| *
That worked for me.

Resources