Wordpress rewrite (remove) part of url - wordpress

I searched around for an answer. There's a couple that look like this, but most point to a plugin, or feature examples that I can't get working in my case...
So the question:
I created a custom post type for the homepage of my site. I made it the default front page, so now when I enter: localhost/vhh I automatically get forwarded to this page: localhost/vhh/homepage/homepage
The first "homepage" refers to the custom post type. The second is the name of the "post".
Now I want to remove both, or at a minimum 1 "homepage", so I get:
localhost/vhh or localhost/vhh/homepage
Can this be done with a WP rewrite rule? And how?

Related

Is WordPress ignoring segments of Permalink?

I discovered a strange behaviour of WordPress in the usage of permalinks. On my local machine set the permalinkstructure to:
/%test%/%year%/%monthnum%/%day%/%postname%/
The first part, "test" is a custom taxonomy.
This results in the following working linkstructure.
http://localhost:8080/reisbowl/2021/06/22/c/
But also, when use the url,which does not exist, the link is working and showing me the correct post "c".
http://localhost:8080/hamburger/2021/06/22/c/
How is that? Is WordPress ignoring the custom taxonomy segment in the permalink or everything but the title?
The question poped up because i have to rename the "reisbowl" tag (just an example) to something else and secure the redirection to the new url. But it seems that this is not any issue.
But why? I just want to understand that.

I don't know why Wordpress worked follow that way

I have a problem with a Wordpress permalink.
These are my steps to show the problem:
1) in admin > pages: I create new page call "GoodMorning" and I have slug "goodmorning", on frontend, I run ://my_domain/goodmorning will show the content of this page
2) Now, I run ://my_domain/find-me/goodmorning it will auto redirect to ://my_domain/goodmorning
/find-me/ : this is any name and this text does not exist on wordpress slug, category, page, post, ....
Please let me know why. I want it to show a 404 page when I run ://my_domain/find-me/goodmorning.
Thanks All.
Adding this (to functions.php) should stop that redirect.
remove_action('template_redirect', 'redirect_canonical');
If you look at the documentation for redirect_canonical here:
Will also attempt to find the correct link when a user enters a URL that does not exist based on exact WordPress query. Will instead try to parse the URL or query in an attempt to figure the correct page to go to.
I assume that's what you're trying to prevent based on your question.
If you need the other functionality of redirect_canonical you can just cancel the redirect by returning false to this filter, like so:
add_filter('redirect_canonical', '__return_false');
You need to flush your permalinks configuration afterwards:
In the main menu find "Settings > Permalinks".
Scroll down if needed and click "Save Changes".
Rewrite rules and permalinks are flushed.

WordPress Page URL rewrite Issue

I need to rewrite wordpress URL.
Currently My URL is : http://www.example.com/about/?pagekey=team1
and I need like ; http://www.example.com/about/team1
Here About is one page template and I've setup for wordpress custom parameter so, when you pass pagekey to this page it will take key and based on that display result.
Now, I need to show user friendly, can anybody have idea how to do this.
currently my parmalink is set to "PostName".
Thanks.
You should follow the WordPress codex here and create 2 files:
page-about.php
single-about.php (this one will be for the teams)
you should create CPT (custom post type) with all the custom post type fields you need for this eg.
Name,
Title,
Position,
Image,
Tag,
Taxonomy,
.... etc
then you will pass the custom post type query (the query) before the loop and you will be able to access the single-about.php from page-about.php
that'all.
Cheers and hope this helps

set wordpress post's permalink for specific page

When I add/edit post I can change slug of the permalink. But I can't add slashes.
So assume I have
http://domain.com/myslug
but I need
http://domain.com/myslug1/myslug2
when I save it does
http://domain.com/myslug1myslug2
How can I overcome this?
When you visit a page, let's say domain.com/careers, WordPress has to figure out which post to retrieve from the database.
It will look at all of its rewrite rules and try to find one that matches the request. In this case, the last rule will catch it:
[(.?.+?)(/[0-9]+)?/?$] => index.php?pagename=$matches[1]&page=$matches[2]
It will pass in careers ($matches[1]) as the pagename value, dissect it accordingly, and then look in the database for a post called careers.
When you visit a page called domain.com/careers/internships, the last rule will apply again. This time it will pass in careers/internships, dissect it accordingly, set the parent of the post to careers, and then look in the database for a post called internships that matches the parent.
If WordPress were to let you set the slug to myslug1/myslug2, a couple assumptions WordPress makes would be wrong.
There is no parent page called myslug1
There is no page called myslug2
There is no rewrite rule that lets WordPress know how to handle a myslug1/myslug2 slug
All that said, it may still be possible to accomplish what you want, but maybe not in the way of making up slugs will-nilly. There has to be a pattern, some kind of rule, that you can give WordPress that says "if x, then y".
For example, you could have a meta box with an input field that whenever you entered something in it, that slug would appear at the end of the URL. Or perhaps you can sandwich a custom slug between domain.com and myslug2.
Here are a few things to look into:
add_rewrite_rule function - Allows you to specify additional rewrite rules
add_rewrite_tag function - Make WordPress aware of custom query string variables
query_vars filter - Allows query vars to be manipulated prior to executing the query
template_redirect action - Executes just before WordPress determines which template page to load
Another approach might be to create a parent page with myslug1 and then a child page with myslug2. Or, depending on your needs, you may also take a look at custom post types. You can have a custom post type called myslug1 and then posts with in it, where one post would be myslug2.
If you have specific questions about implementing one of the aforementioned, look through Stack Overflow or post another question with details if you can't find the answer.

Wordpress, two URLs for the same post

Is it possible to have two URL for a same post in wordpress.com?
One would be a short URL like
http://mydomain.com/28
and the other will be a SEO friendly one:
http://mydomain.com/28-this-is-the-post-about...
thanks
In wordpress, all posts are at the URL mywordpresshome.com/?p=xx where xx is the post ID number. Anything else involves mod_rewrite. WordPress will do one type of rewrite for you in the Admin section -- so you can pick either the short version or the long one above. In the Admin interface, it won't let you do both. But you could edit mod_rewrite yourself and effectively get both patterns -- short and long -- to work.
Here's info on mod_rewrite: http://www.workingwith.me.uk/articles/scripting/mod_rewrite
In the Settings->Permalinks part of the admin panel, you can set the look of your permalinks, but that doesn't mean the default dynamic links (yourblog.com/?p=5 or whatever) will stop working. You can use those links too and they will take you to the corresponding page. However, Wordpress will automatically substitute the dynamic link by its SEO-friendly alias.
I'm not sure whether it's possible to have two different aliases for the same post though.

Resources