If the page 404s, then 301? - wordpress

On a wordpress site.
I'm updating the permalinks so that the posts go to /blog/title-of-post instead of right off the route. Problem then becomes all 400+ posts that are on the site will 404.
Rather that add a 301 redirect in the htaccess file for each of these, is there a better way?
Can I set the htaccess to detect the url, and if it 404s, to instead send the traffic to url.com/blog/the-rest-of-the-title, so that it passes the same url string but with /blog/ in front of it?
Hope this is clear, please let me know if there's any questions.
Thanks in advance/

this is possible only on apache 2.4+ . On apache 2.4 you can pass the 404 URI string to the destination url of ErrorDocument.
ErrorDocument 404 http://example.com/blog%{REQUEST_URI}
This example will not work on lower versions of apache as they dont understand the %{SERVER_VARIABLE}

Related

How to Setup Redirection Fallback If Subdomain URL Doesn't Already Have One

We have a subdomain (oldblog.oursite.com) and we've set up over 600 redirects to (newblog.oursite.com), however, there are likely some live links to pages on oldblog.oursite.com that we haven't caught and implemented a redirect.
Is there a way to make oldblog.oursite.com redirect any URL that doesn't already have a redirect rule in place to redirect to the index of newblog.oursite.com? Preferably in the .htaccess file?
This should work if you put it last:
RedirectMatch 301 "^/" http://newblog.oursite.com/
Please test it. Any problems let me know. Take care that you are testing server generated redirects and not ones cached in your browser. Using 302 redirects during testing can help.

How can i redirect some url in wordpress?

I am working on word press blog
I want to redirect some old URL to new one's.
My old URL was www.example.com/blog/category/subcategory/post.
Now i want to redirect all these types of URL to www.example.com/blog/Post.
I have tried redirect in ht-access of sub domain but i didn't worked.
Redirect 301 category/subcategory/test_post/ www.example.com/blog/test_post/
Thanks in advance
Your idea with htaccess was correct. You will have to implement this via the Apache Conf. So htaccess will add its content to the Apache configuration.
However, not all Apache servers are configured to accept all htaccess commands.
So, please check your main Apache config if it allows to override anything and if the redirect module is enabled.
Also note, that it is not a good idea to write host names in capital letters, as they may be case sensitive. www != WWW
As for the syntax, you might need to include the protocol.
Redirect permanent /one http://www.newdomain.com/two

Strange URLs from wordpress - can't 301 redirect

I am finding that Google is indexing some very strange URLs from my Wordpress site
The page URL is:
https://www.bennettsfunerals.co.uk/arranging-a-funeral/your-guide-to-arranging-a-funeral
However Google is finding URLs like:
https://www.bennettsfunerals.co.uk/arranging-a-funeral/your-guide-to-arranging-a-funeral/?pagename=arranging-a-funeral%2Fyour-guide-to-arranging-a-funeral
any attempt to 301 redirect this URL is failing and i cant find the root cause the problem
is this something anyone has seen before and how can i get around this problem
THANKS!!!!
Try using this Javascript redirect.
// Javascript URL redirection - generated by www.rapidtables.com
window.location.replace("https://www.bennettsfunerals.co.uk/arranging-a-funeral/your-guide-to-arranging-a-funeral/");

301 redirections when migrating from Wordpress to Ghost

I want to migrate my WP blog to ghost, the permalinks have the same slug (/blogWP.com/title-article and blogGhost.com/title-article) but I still have pages such as blog.com/category for instance to redirect.
Is there any way to make 301 redirections in Ghost as I would do in a .htaccess file ?
Thx !
It's possible to redirect through ghost itself directly, but you need to alter the core. Though I'm not sure it's the perfect or proper way.
open core/server/errorHandling.js
find this line:
error404: function (req, res, next) {
Add below:
res.status(301);
res.set({'Location': 'http://your-new-wordpress-blog-url'+req.url});
res.send('301','Not found');
This way, Instead of showind 404s, I redirect to my wordpress' new domain (http://your-new-wordpress-blog-url in the example)
This way, www.ghost.url/not-a-valid-page will redirect to http://your-new-wordpress-blog-url/not-a-valid-page instead of showing the Ghost's 404 page.
p.s: This redirects, but I'm not 100% sure the headers are correct, I'd appreciate if someone else would clarify.
I'm sorry, but there is currently no way to do redirects with Ghost.
The best way to do redirects is to use a proxy server (recommended) and do the redirects there. For Apache you could use .htaccess files and nginx offers HttpRewriteModule.
My ghost blog is a subdirectory /blog/ runing with NGINX.
I needed to redirect old wordpress urls /blog/category/post-title to /blog/post-title
Except for /blog/ghost/.., /blog/tag/.., /blog/author/.., /blog/post-title/amp/.
So I came with that rewrite rule :
rewrite ^/blog/(?:(?!ghost|tag|author).).*/(?:(?!amp))(.+)$ /blog/$1 permanent;

.htaccess: Codeigniter + Wordpress

I am developing site on Codeigniter and want to "connect" it with Wordpress.
So the URL structure will be
SITE-URL
-SITE-URL/folder1/
--SITE-URL/folder1/some-link
-URL/folder2/
...etc
What I need to do, is when Codeigniter throws 404 error, the content is taken from Wordpress website(same relative URL) without any redirects for end user.
Thanks
Not sure you can do this with .htaccess since .htaccess can only do re-directions before the content is served, meaning before Codeigniter has responded to the request. So when Codeigniter generates a 404, it's returning content an .htaccess isn't processed.
What you would need to do is generate a 404 controller that either does a redirect, which you don't want, but would be the easiest. Or the 404 controller would then have to load in wordpress and process the request through there. Something similar to proxy, but it will need to be done with PHP.

Resources