I need to create a site that will allow for a unique user-identifying number to go in be appended to the URL.
So for instance
test.com/12345/about/
will load the page with slug 'about' (not redirect to it).
I have tried the following:
function custom_rewrite_basic() {
add_rewrite_rule('^([0-9]+)/', '/$matches[1]', 'bottom');
}
add_action('init', 'custom_rewrite_basic');
But it does not work, any prefix to the URL redirects the page with or without the block of code above.
So in my tests
test.com/23423423/about will re-direct to test.com/about which is not what I want.
Is what I am trying to achieve possible? If so, could anyone offer advice as to how I would best go about implementing it?
Related
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');
I've got a Wordpress site set up with a custom post type and custom taxonomy and custom URLs. However when I go to:
/custom-post-type/custom-taxonomy/custom-taxonomy-sub/postname
It 404s. I've tried everything and just want to create a redirect that grabs the postname and passes it to single-custom-taxonomy.php without changing the URL.
Some sort of redirect with a regex like /custom-post-type/(.*)/(.*)/(.*) and pass the third match to single-custom-taxonomy.php as a variable so it can get the post page.
How do I do this?
Figured it out:
function custom_rewrite_basic() {
add_rewrite_rule('^knowledge-center/(.*)/(.*)/(.*)', 'index.php?pagename=post_type=questions&questions=$matches[3]', 'top');
}
add_action('init', 'custom_rewrite_basic');
I am looking to redirect all the posts and not pages and custom post types from old domain to new domain. So all posts for instance
http://www.example.com/post1 should go to http://example1.com/post1
http://www.example.com/post1 should go to http://example1.com/post1
The pages i.e. http://www.example.com/page1 should still be functional
I can find alot of answers on moving blog into sub directory along with moving all the links but not for moving just the posts and not the pages
Edit
There are over 1000 posts so I am looking for a dynamic way of doing this
Thank you for your help
The hack I finally end up using was
Add ob_start to single.php as first line even before get_header()
Add following code in functions.php
change example.com with your site domain name
add_action('wp_head','redirectme');
function redirectme() {
if ('post' === get_post_type()){
wp_redirect( 'http://example.com' . $_SERVER['REQUEST_URI'] , 301 ); exit;
}
}
I don´t think this is possible with this URL schema. .htaccess (or the Apache behind it) doesn´t know if post1 is a post or page. How shall it then redirect on this base?
If you would change the URL structure for pages alone (I don´t think this is possible by default, or am I wrong?) you could target their URL scheme and redirect.
To access the old pages (with the new URL) you could then use a "broken link checker" plugin.
Update: You could possibly use the hook described here to check wether the called post is a post or page and then use wp_redirect to call your new URL.
Hoping someone can help. I don't want to create actual pages in the backend of wordpress but i want to know how i can make it so if anyone goes to a url: mydomain.com/page/sub-url.
Then i can grab that "sub-url" and output a page with content i generate via php.
If i grab that "sub-url" and i don't want to output i can do a 404 error.
I want to try to stay away from editing the htaccess file is possible but if i need to i can.
Everytime i try search for this, i get result for creating actual pages in the backend automatically which i don't want.
I think what you're looking for is a custom rewrite endpoint, which you call 'page'. Then on yourdomain.com/page/sub-url, 'page' is the parameter and 'sub-url' is the value. You can get that value with the function get_query_var().
After applying, be sure to go to your Wordpress admin->Settings->Permalinks, and click save to flush the current rewrite rules.
Update:
Use this code to show a 404 page
global $wp_query;
$wp_query->set_404();
status_header( 404 );
get_template_part( 404 );
exit();
(source)
I'm trying to have custom variables in my URL for Wordpress site. I have read up as much as I could find on the subject and so far have the following in my functions page:
function add_query_vars($aVars) {
$aVars[] = "mrdrct";
return $aVars;
}
// hook add_query_vars function into query_vars
add_filter('query_vars', 'add_query_vars');
And the following on my header page:
if(isset($wp_query->query_vars['mrdrct'])) {
$mVar = $wp_query->query_vars['mrdrct'];
echo "variable is $mVar <br />";
}
Just to test out if things are being passed correctly and they are. However, when I use a link with the url variable in it - say www.mydomain.com/?mrdrct=myVarable - I am not directed to my homepage of my Wordpress site which is set to a static page with a template on it - I am instead directed to a page with my latest posts on it. I cannot figure out why this is happening - any ideas? Hopefully I've explained this well enough.
Thanks.
When WP sees a query string (? after the URL) it will attempt to display matching posts using it's rewrite rules. If no posts match it will show a 404 error - I would guess you do not have a 404.php file, so WP is showing the default which is index.php (see the Wordpress Template Hierarchy for more details on that).
I'm not 100% sure what you want to achieve, but I'd suggest that you need to look at changing the query when $wp_query->query_vars['mrdrct'] is set. See the WP Codex for query_posts() for a good place to start, if you are not already familier with it.