.htaccess maker

.htaccess generator maker easier

404 page redirect to homepage to fix 404 errors in WordPress

Some times you can not edit your htaccess file to redirect all 404 error pages to homepage in WordPress, this is may be because you host your blog on a free server, or you have not a permission to edit this file, however, there is still solution to this issue, follow the following steps to make 301 redirect correctly:

  1. Login to your blog and go to the Dashboard.
  2. Go to Appearance menu and choose the item Editor.
  3. The page contains file name in the right side, find the file 404 Template (404.php) and click the link.
  4. You will see the code for the 404 page in your theme, at the head of the code and just after the tag <?php put the following lines.
header("HTTP/1.1 301 Moved Permanently");
header("Location: ".get_bloginfo('url'));
exit();

Note that you will need to clear cache if you have a cache plugin installed, after that, go to home page and try and page that is not in your site and check the result.

How to make 301 redirect in PHP using header() function

As you can set up 301 redirect using htaccess, you can also make this type of redirection using PHP, this can be done using the function header() in PHP, the function header() is used to send a raw HTTP header, so we can make use of it to send redirect info in HTTP protocol, suppose that we want to redirect the current page to the new domain htaccessredirected.com, the following code will do it.

<?php
header(“HTTP/1.1 301 Moved Permanently”);
header(“Location: http://www.htaccessredirected.com“);
exit();
?>

Remember to use this code in the head of the page, and before any echo or output.

Htaccess Rewrite URLs Using Rewriterule , how to?

What is Rewrite URLs?

Some times you need to have a SEO URLs in your site, if your link structure is not SEO friendly like for example in WordPress default link structure www.htaccessmaker.com/?p=1 for posts or www.htaccessmaker.com/?cat=3 for categories, this links scheme is not SEO friendly because when you see www.htaccessmaker.com/?p=1 you will not know any thing about the content of this page, but suppose if you have a rewrited URL like www.htaccessmaker.com/htaccess-301-redirec/ by seeing this URL you will know that this page will talk about how to make 301 redirect using htaccess file.

How to make htaccess Rewrite URLs ?

A famous example about how we could force the redirect of the non www URLS to www URLS in a specific site, suppose we have the site name htaccessmaker.com, it will be like this:

RewriteCond %{HTTP_HOST}   !^www\.htaccessmaker\.com [NC] 
RewriteCond %{HTTP_HOST}   !^$ 
RewriteRule ^/(.*)         https://www.htaccessmaker.com/$1 [L,R]

Note that to use the Rewriterule you must do the following things first:

  1. Use the Tag <IfModule mod_rewrite.c> in the beginning of the rewrite code and  </IfModule> at the end.
  2. Start bu activating the RewriteEngine by setting it to on.
  3. Determine the RewriteBase for you rewrite, for example “/”.

A full example is to rewrite URLS for WordPress Blog:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

  • 1
  • 2
  • 3
  • 4
  • Next Page »

Copyright © 2021 · htaccessmaker.com · Log in