.htaccess maker

.htaccess generator maker easier

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.

301 redirect htaccess in simple way

Htaccess is a configuration file that contains some rules, therefore syntax errors such as missing letters can lead to inability to work correct and it may returns HTTP Error 500 Internal server error. Your first step towards the creation of 301 redirect through the use of htaccess file is to ensure that your FTP is configured to reveal all hidden files. The steps in making redirect using htaccess are:

  • Create an empty text with the use of a simple Windows text editor such as notepad, and save it as .htaccess without any name just the extension.
  • Add the redirect code below to the created file and save it.
  • Upload your created file to the root folder. If you are using a text editor, make sure the file is saved as a plain text. You can also save the file into other paths in case there are needs to.

Notes:

  1. If you have already the file on your server, download it and edit it using notepad, add the redirect code and then upload it again.
  2. To upload the file using FTP, you can use the free FTP client FileZilla, it is free and open source.
  3. You can use web file manager or editor like Cpanel file manager instead of FTP client.

The following code can do the desired 301 redirect for you:

Redirect 301 old_url  new_url

A real example about the 301 redirect is

Redirect 301 /about.htm http://www.clogica.com

In the above example we redirected the page about.htm to the new domain clogica.com

Your .htaccess File

Download Copy

You can create the .htaccess file using a good text editor like TextPad, UltraEdit and Microsoft WordPad. You cannot use Microsoft NotePad. An .htaccess file can contain a single or multiple lines depending upon the functions you want to include in it. You will need to use an FTP (File Transfer Protocol) program to upload the file. Also you must to upload the file in “ASCII” mode. Some FTP programs use “BINARY” as the default to upload files. If that’s the case you must change it to “ASCII”. Upload the file to the directory you want it to reside in. when you look at the files in your website’s directory you might not see it as your web server’s software is probably hiding it.

Redirect 301 old url to new url

Redirect 301 old domain to new domain

Enter full path for new domain

Force Redirecting non-www to www with .htaccess

Some times you need to redirect all non-www requests to www for your site, this will be useful to avoid duplicate content, as the search engines may crawl tow copies of the same page, one with www and the other without, to get ride of this problem, you can use 301 redirect to tell search engines that the non-www page is the same as www one.

Making this type of redirect is easy as a piece of cake, just use the following lines in your .htaccess file, where website.com is your domain name.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^website.com$
RewriteRule (.*) http://www.website.com/$1 [R=301,L]

If you want a generic solution regardless of the domain name, you can use the following lines:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

This will work for any domain.

Your .htaccess File

Download Copy

You can create the .htaccess file using a good text editor like TextPad, UltraEdit and Microsoft WordPad. You cannot use Microsoft NotePad. An .htaccess file can contain a single or multiple lines depending upon the functions you want to include in it. You will need to use an FTP (File Transfer Protocol) program to upload the file. Also you must to upload the file in “ASCII” mode. Some FTP programs use “BINARY” as the default to upload files. If that’s the case you must change it to “ASCII”. Upload the file to the directory you want it to reside in. when you look at the files in your website’s directory you might not see it as your web server’s software is probably hiding it.

Rewrite none-WWW to WWW​

Redirects example.com to www.example.com

Copyright © 2021 · htaccessmaker.com · Log in