Setting Rel="Canonical" on each wordpress post - php

Im struggling to get the concept of using rel=canonical with wordpress. Ive done as much research as I could on the topic.
Backgound:
I would like to move 100 existing posts to another (new) domain BUT also keep the posts on the existing (old) domain!
I would like the posts of the new domain to appear in search results eventhough they were originally indexed on the old domain
Webmasters.stackexchange said this should not be a problem providing I use rel="canonical"
I would like to code the rel="canonical" without making use of plugins as I understand it I need to add the code to the header.php section of the old wordpress site.
<?php if ( is_singular() ) echo '<link rel="canonical" href="' . get_permalink() . '" />'; ?>
Numerous sources all point towards the above code having to be added to header.php in wordpress
My Question
Will the above code provide a rel="canonical" to all existing pages and new pages in the future?
Where in the above code do I specify which site I would like to credit with the posts (the new site)?
Sources
https://support.google.com/webmasters/answer/139066?hl=en
https://thomas.vanhoutte.be/miniblog/add-a-canonical-tag-to-wordpress-header-php/

rel="canonical" attributes credit of current page content to the URL specified in the href. Made that clear, you should be using canonical link tag on the single post pages in your new site. (NOT the old site, unless you plan to take down the old site in future and keep the new site only).
Answer to your first question
Technically that code will render the canonical link tag, but it will be useless in search engine perspective. Because, the function get_permalink() will output URL of the current post, which actually is not expected in your case. The canonical link tag in your new site should be as follows
<link rel="canonical" href="http://www.youroldsite.com/respective-post-slug/"/>
Answer to your second question
In your code value of the "href" attribute should be the target link. (Instead of get_permalink())
I am wondering why you don't want to use a plugin!!! This is like reinventing the wheel. "All in One SEO" and "SEO by Yoast" are from those few good plugins for enhancing SEO of your website.
Content of the posts in your old site is already crawled and indexed by search engines. Thus you should not attribute its credit to new website. I hope this clears your concept and answers your questions.

Related

How to add <a href> to link other post in Wordpress

I am new to wordpress, I have searched this thing on net but unable to find exact solution.
I have created a post that contains the Actor's Profile and the list of Movies Actor has worked in. Each Movie has also a different Wordpress Post that contains Movie details.
Now i want to create a link on each Movie, by which user can view the details of that particular movie.
This following link gives the result
<a href="http://localhost/wordpress/2015/10/09/movie-main-page">
but if i add this in each post, then it would be very difficult for me in future, when i will be uploading the site on web server, to change each link individually on every post.
I believe that there must be some way out there better than what i doing here, but somehow i am unable to find that trick.
Kindly guide me.
Thanks
You can use the get_site_url() template tag to return the site url and concatenate into the string. I haven't tested this code, so it might need some tinkering, but it should get you started:
<?php echo '<a href="' . get_site_url() . '/2015/10/09/movie-main-page">' ?>
wordpress link to anchor on another page
If Your Movie Page URL is Same Then You can use
echo get_permalink('11');
Here 11 is id of the page/post.
or if you have different different post for movies that can have different urls.
You can use wp_query loop for each post
[https://codex.wordpress.org/Class_Reference/WP_Query][1]
and use simply **echo get_permalink();**
There are two ways to achieve what you're doing.
The first and easiest way would be to run a search and replace on the database to change any URL's in your posts to the new URL. You should have a look at https://interconnectit.com/products/search-and-replace-for-wordpress-databases/ .
You could also use Advanced Custom Fields to Achieve this. You would need to use the post object field to link to another page within wordpress. With the post object field you can create a field for each post that links to another post which you can then display in your template. Take a look here https://www.advancedcustomfields.com/resources/post-object/. With this method you could transfer all the content and associated links would still work.

SEO duplicate content issue with alternative URLs

I have a PHP website where every page can be accessed either by page ID or by page name:
http://domain/page_id=ID
http://domain/page=NAME
The problem is that Google treats this as duplicated content. What is the best practice to avoid duplicate content in the case? Will 303 redirect will be better than entirely avoiding two different URLs to lead to the same page?
According to Google:
In the world of content management and online shopping systems, it's
common for the same content to be accessed through multiple URLs.
Therefore,
Indicate the preferred URL with the rel="canonical" link element
Suppose you want
http://blog.example.com/dresses/green-dresses-are-awesome/ to be the
preferred URL, even though a variety of URLs can access this content.
You can indicate this to search engines as follows:
Mark up the canonical page and any other variants with a
rel="canonical" link element. Add a element with the attribute
rel="canonical" to the section of these pages:
This indicates the preferred URL to use to access the green dress
post, so that the search results will be more likely to show users
that URL structure. (Note: We attempt to respect this, but cannot
guarantee this in all cases.)
So, all you need to do is to add the canonical link element to the <head> section of your pages with absolute paths.

Generating dynamic HTML pages with PHP

I've been stuck up on this topic for a while. As a project to help me learn MySQL, PHP, and database security a while back, I created a lightweight blog system. It blew up on me and a few of my fellow programmer buddies wanted to use it and now I am working on improving on its features.
The way it works, simply put, is that a blog post is uploaded, the title, author name, and date are stored in a database, but the actual content is used to dynamically generate an html page which is where the actual blog post is viewed. In the database, the path to each blog post is saved so that it can be used to access and view the blog post page. One of the features of my blog system is that the generated HTML is fully customizable, so you aren't limited by a specific template. The way I am currently doing that is by using a function that takes parameters of the values and then returns a HEREDOC with the format desired, which is then written to a file whose name is based on the post title.
function get_full_post_html($title, $author, $date_posted, $text) {
return <<<EOT
<!DOCTYPE html>
<html>
<head>
<title>$title</title>
</head>
<body>
<h1>$title</h1>
<h3>$author</h3>
<h4>$date_posted</h4>
$text
</body>
</html>
EOT;
}
Not the cleanest solution, which is one of the reasons I am looking for a better way. The other reason why I need to rethink this, is because I am currently adding support for comments, which means the html page that is generated now needs to be dynamic, with my PHP method for getting the comments. Whereas before the blog posts were static content, now with comments enabled, the page becomes dynamic.
I had a look at this question, but frankly the answers were vague and didn't make much sense. The main question I am asking is, how are dynamic pages typically generated in PHP? For example, take this blog post on A List Apart: http://alistapart.com/article/tweaking-the-moral-ui. It has a physical page for the post, tweaking-the-moral-ui, but still has dynamic features like comments, ads, etc. How is this done?
Here is the link to this entire project on GitHub, if you are interested in understanding how it works in depth.
I think there is a starting mistake here.
You are writing pages to file to have a nice url instead of using a rewriteurl logic.
if you want to make them "dynamic" you can regenerate the file every time a user add a comment.
Or follow this answer of another question:
http://stackoverflow.com/questions/16388959/url-rewriting-with-php
So basically you add comment to your blog platform, and you regenerate the page once per request as many platform do. In your index.php you implement some logic to research from the url the post you need.
The pages will be visible because linked in some listing. In the listing instead of:
http://myblog.com/mypost.php?id=33
You will have
http://myblog.com/i love my dog/
Then in your index.php you will analyze
$_SERVER['REQUEST_URI']
and serve the correct post to the user or to the web crawler of search engine

Change facebook meta data based on URL / Anchor

I've discovered whilst typing my question a small exchange on this at Adding a URL hash into meta data for Facebook and Twitter share cards which suggests that this may not be feasible but as things change over time I thought I'd seek guidance.
The scenario:
I have a URL http://example.com/ which includes appropriate Facebook meta in the header. I'm most interested in:
<meta property="og:image" content="whatever.jpg" />
At a later date I may add some additional info on the page and link to it with an anchor - e.g. http://example.com/#new and may want to display different meta for that anchor, e.g.
<meta property="og:image" content="whatever-another.jpg" />
My question is, is there any way to dynamically change the meta depending on whether the URL shared is the original URL or the URL with an anchor. The page could have multiple anchors.
My site uses Wordpress so I can use things like custom fields, etc if they are a possible manual solution but whatever, the ability to change the meta field based on the URL anchor is the goal.
Appreciated any guidance.

How to prevent duplicate title tags on dynamic content

Links on the website I am making currently look like this:
http://www.example.net/blogs/151/This-is-a-title-in-a-url
My php system pulls out the id (151 say) and uses that to pull to content from my database. The text afterwards is effectively ignored (much like stackoverflow uses).
Now my problem is that this creates duplicate titles that Google will sometimes index and I lose SEO as a result:
http://www.example.net/blogs/151/This-is
http://www.example.net/blogs/151/
What is the best way to make it so that google and other search engines only see the correct full link so that I don't end up with duplicates and get the best ranking possible?
EDIT: I notice that with stackoverflow site that you get dynamically redirected to another page? How do they do that?
Pick a URI to be canonical.
When you get a request for http://example.com/123/anything then, instead of ignoring the anything, compare it to the canonical URI.
If it doesn't match, issue a 301 Moved Permanently redirect.
A less optimal approach would be to specify the canonical URI in the page instead of redirecting:
<link rel="canonical" href="http://example.com/123/anything"/>

Categories