Wordpress Blog Post Redirect (1000 posts) - php

I have a wordpress blog with the following URL structure:
www.mysite.com/2016/09/the-name-of-the-post/
However i noticed that for SEO Reasons it would be better to have a structure like:
www.mysite.com/blog/the-name-of-the-post/
I have around 1000 posts with the "old" url structure. I can change the url structure in wordpress. However i would need a 301 redirect if someone tries to access a post using the old url.
E.g.
person/ google bot tries to access page via /2016/09/the-name
-> 301 Redirect to /blog/the-name
What is the best practice to do so? Do I add 1000 lines to the .htaccess file and do redirects? Would that negatively influence the server response time cause apache has to check through a long list of redirects first?
Can I tweak a file in the wordpress file structe which checks, if the requested page is included in e.g. an array, it will redirect it to the new url?
Thank you very much for your suggestions

You can simply change the URL structure in WordPress to the one you want to have. You don’t have to modify your .htaccess or anything else.
WordPress will understand which post is referenced by the URL and redirect to the new canonical URL automatically.
Just go to the Permalinks admin page and select the “Post name” option as described in the Codex.
However, if you’re introducing a prefix like /blog/, too, you must redirect to the new URL base through .htaccess, e.g.
RedirectMatch 301 ^/[0-9]{4}/[0-9]{2)/(.+)$ http://example.com/blog/$1 // taken from stackoverflow.com/a/42211746/

You can use the following Redirect
RedirectMatch 301 ^/[0-9]{4}/[0-9]{2)/(.+)$ http://example.com/blog/$1
This will redirect all urls from this form /1234/12/foobar to this /blog/foobar

Change the permalink to the new structure and add this in your .htaccess
RedirectMatch 301 ^/([0-9]{4})/([0-9]{2})/(.*)/$ http://example.com/blog/$3

I had the same problem before but I resolve this issue using simple 301 redirects plugin.
https://wordpress.org/plugins/simple-301-redirects/
You can bulk upload 301 redirects using bulk upload add-ons of this plugin.
Hope this will help you.

Related

What is Google reaction to indexing redirect URLs?

i just installed a CMS script to make my website that it give me URLs like this :
site.ltd/index.php?page=ID
after some changes in URL, i used a .Htaccess code to remove index.php from URL. But this code do not remove index.php from original URLs. it just redirect index.php to none index.php.
For more understanding read this :
after i used this code, all of my post URLs still looks like this :
site.ltd/index.php?page=ID
But when i open this url, it will redirect to this :
site.ltd/?page=ID
And main question is:
in the source of my website, URLs have index.php, but after opening, transferred to the new address (without index.php) , Do not create a problem?
i mean this permanent transfer links (redirect index.php to none index.php for every links) do not make problems in decline in popularity and Google's robots crawl the site? and can google index my none index.php URLs and ignore URLs with index.php?
And if you can: Give me a code that can remove index.php for ever in my URLs without redirect needed!
Sorry for my English.ENG in not my main Language :D
Well, if the redirection is 302 (temporary redirection process); then your site would be in trouble.
Google suggest us to apply 301 (permanent redirection) so that not only visitors; but also Search Engine Robots can also check the new url and index that.
Hope it helps.

404 page redirects and SEO

I hope this is OK to ask here. I am almost ready to upload a new theme to my blog. Which after some serious consideration and research in SEO I have decided to restructure some of the pages, their child pages and to remove some of the categories.
Unfortunately this will result in various 404 results which will be bad for my site.
What I want to do is this:
On the 404 page, before it loads get last parameter of url, so for example: example.com/parent/child-page-name/
Then perform a check in wordpress using get_page_by_title().
If the page exists get the page permalink then use php header location to send 301 redirect to the new page.
if not, display 404 page with search options etc..
Is this a good way to handle this? is 301 the correct redirect?
To answer your redirect question, yes the 301 redirect is the correct one to use as it passes on the link equity from the last page.
Rather than using PHP to automatically solve your 404 problem when the new blog is launched, I'd crawl your website first and manually redirect all the old links to the new ones. It's tedious, but it will make sure nothing slips through the cracks that an automated process may otherwise miss.
A good way to do this is to crawl your site as it is at the moment, put all the links into a spreadsheet and put the new urls into the next column. From there you can concatenate the urls into a rewrite rule for the .htaccess file.
To show you what I mean, I have set up a basic sheet you can use to help you out.
https://docs.google.com/spreadsheets/d/1htHq0oeATsfrFJpAxKg0_e5dJqSmJ_idrrH-tudkuq4/edit?usp=sharing
Source: Past experience, Commercial SEO Technician for 2 years

404 Page is generated instead of proper 301 redirect to new post

I changed permalink from /%postname%-%post_id%/ to /%postname%/ only. But the problem is that I can't get any method to redirect 301 old permalink to new one.
I searched google and found this plugin which redirect the old permalink to new one. When I check the HTTP Response Header, it also shows the old page is being redirected to new one but actually in the content it shows 404 page. Here is the example page.
Even when I try to fetch the page in Google webmaster tools, the bot fails to fetch the old page.
Is there any way to fix the problem??
Quickest way is just add some redirect into your .htaccess file, if you use Apache server.
You can easily populate the url list for your legacy links by write some sql query.
.htaccess
# BEGIN FIX URL
Redirect /my-sample-post-100 /my-sample-post/
Redirect /my-2nd-sample-post-101 /my-2nd-sample-post/
...
etc
# END FIX URL

.htaccess & wordpress site - How to add 301 redirect rules for no longer valid permalink?

I recently did a big update on my wordpress site, thus some old permalink are invlid now.
Seach google for a tutorial on this and found a lot about static html page redirection examples or specific php page which are not what i'm looking for
Both my old and new permalinks are in path format, for example, i need redirect a couple urls of old gallery posts (deleted) which are
/2009/06/gallery/abc/
/2009/06/gallery/cba/
/2009/06/gallery/bbc/
/2009/06/gallery/aab/
to a new page which is
/gallery/
How do i write a correct redirect rule for this?
RewriteRule ^[0-9]{4}/[0-9]{2}/gallery/.*$ gallery [R=permanent,L]
Should get you to where you need to be. If you need the actual parameters (IE Date / Month / Title) passed along it is a bit of a change up, but do-able.
EDIT
Fixed typo.
Try something like this:
RewriteEngine On
RewriteRule ^/2009/06/gallery/\(?([^/]*)\/$ /gallery/ [R=301,L]
Personally, I would use the Redirection plugin rather than editing your .htaccess directly -- it's easier, less danger-prone, and will let you log what redirections have been happening. It will also track 404 errors so you can see if you've forgotten to redirect anything.
Then, if you're just redirecting a couple of fixed posts, it's just a matter of adding a simple rule for each redirect, with the old and the new URLs, on the Redirection configuration page. You can also use regular expressions, as with .htaccess, if you want to do anything more complicated.

Changing WordPress URL structure while maintaing the proper 301 redirects with mod_rewrite

I currently have a blog set up with WordPress using URLs like so:
www.domain.com/blog/?pid=384092817
This was before I knew anything about anything. There's a significant amount of content there now and I want to change my URL structure to:
www.domain.com/my-post-title-384092817
Is there a way to set up my .htaccess and mod rewrite so when users go to the first url they get a 301 redirect to the second URL? I know to change my WordPress address (URL) setting from www.domain.com/blog to www.domain.com and my permalink setting to /%postname%-%post_id%, but how do I tell the old URLs to redirect to the new ones?
Do you actually mean that when users go to the second URL, it will be rewritten to the first? That can be done with
RewriteRule /blog/.+-(\d+)$ /blog/?pid=$1
If you want to send 301 redirects from the old URLs to the new ones, then you can't really do that with an .htaccess file. The reason is that Apache's mod_rewrite doesn't have access to the title of your post, so it won't know what title to insert in the URL. You'd have to do that redirect with PHP.
EDIT: y'know what, actually that's not entirely true. You can do some pretty crazy things with the RewriteMap directive, such as instructing Apache to ask an arbitrary program to perform the rewriting, and in that way you could have, say, a PHP script that does some database work to figure out what the title is and rewrites the URL appropriately based on that. But that seems like a way overcomplicated solution and I wouldn't suggest doing it - it'll be much cleaner and easier to just send the redirect directly from PHP.
Depending on your WP version, you can just use the Permalink redirect plugin -- should do the trick for you within WordPress and without mod_rewrite. However, as of WordPress 2.3, a lot of this should work automatically. At that point, the only thing you should have to do is redirect all your /blog/... requests to the route, which you can do via mod_rewrite roughly like this:
RewriteRule ^blog(.*) $1 [QSA]

Categories