[.htaccess] Redirecting 404 error with .htaccess via 301 for SEO etc

I couldn't find a straight answer to my question and need to know it from the real experts.

I had a website which urls were generated by Joomla. I believe that tons of urls are around in the search engines and I really don't know which of them all. A 302 redirect would be an option, but I can't say which urls need to be redirected.

The only thing I know that all the urls were generated by a sef404 script, it's a SEO script for Joomla.

My question, how can I make sure that all the orphan urls on google and other search engines are delivered correctly with a .htaccess file?

How do I 301 redirect all 404 pages to the homepage (root document)

At the moment I use a custom 404.html error file, but there are too many files and will give a rollercoaster of custom 404 error pages

This question is related to .htaccess redirect seo http-status-code-301

The answer is


You will need to know something about the URLs, like do they have a specific directory or some query string element because you have to match for something. Otherwise you will have to redirect on the 404. If this is what is required then do something like this in your .htaccess:

ErrorDocument 404 /index.php

An error page redirect must be relative to root so you cannot use www.mydomain.com.

If you have a pattern to match too then use 301 instead of 302 because 301 is permanent and 302 is temporary. A 301 will get the old URLs removed from the search engines and the 302 will not.

Mod Rewrite Reference: http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html


I came up with the solution and posted it on my blog

http://web.archive.org/web/20130310123646/http://onlinemarketingexperts.com.au/2013/01/how-to-permanently-redirect-301-all-404-missing-pages-in-htaccess/

here is the htaccess code also

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . / [L,R=301]

but I posted other solutions on my blog too, it depends what you need really


Examples related to .htaccess

Use .htaccess to redirect HTTP to HTTPs Getting a 500 Internal Server Error on Laravel 5+ Ubuntu 14.04 Server unable to read htaccess file, denying access to be safe Laravel 5 – Remove Public from URL Laravel 5 not finding css files How can I fix the 'Missing Cross-Origin Resource Sharing (CORS) Response Header' webfont issue? How Can I Remove “public/index.php” in the URL Generated Laravel? Apache 2.4 - Request exceeded the limit of 10 internal redirects due to probable configuration error Forbidden You don't have permission to access / on this server Htaccess: add/remove trailing slash from URL

Examples related to redirect

React-Router External link Laravel 5.4 redirection to custom url after login How to redirect to another page in node.js How to redirect to an external URL in Angular2? How to redirect to a route in laravel 5 by using href tag if I'm not using blade or any template? Use .htaccess to redirect HTTP to HTTPs How to redirect back to form with input - Laravel 5 Using $window or $location to Redirect in AngularJS yii2 redirect in controller action does not work? Python Requests library redirect new url

Examples related to seo

Can a relative sitemap url be used in a robots.txt? How do search engines deal with AngularJS applications? What is the purpose of the "role" attribute in HTML? How to request Google to re-crawl my website? When should I use a trailing slash in my URL? Redirecting 404 error with .htaccess via 301 for SEO etc .htaccess 301 redirect of single page Replacing H1 text with a logo image: best method for SEO and accessibility?

Examples related to http-status-code-301

How long do browsers cache HTTP 301s? PHP header redirect 301 - what are the implications? Redirecting 404 error with .htaccess via 301 for SEO etc .htaccess 301 redirect of single page HTTP redirect: 301 (permanent) vs. 302 (temporary) How do I make a redirect in PHP?