PHP .HTACCESS Redirects

We can redirect any webpage to any other or redirect the whole domain or website to another using HTACCESS.

HTACCESS Redirects

.HTACCESS file manages settings for the PHP server. Also, it can manage PHP file routes.  There are various types of HTACCESS redirects, as mentioned below. 

301 redirect

This method is a permanent redirect method used to indicate a search engine to move from the originating URL to a new URL permanently. This method is the best for implementing URL redirect for the website. Here, code 301 represents the HTTP status code for the web.

Note: This method is the most friendly website redirection method for search engines.

Syntax

RewriteEngine on
RewriteCond %{http_host}^yourdomain.com[nc]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [r=301,nc]

Note: Replace yourdomain.com with your domain name.

This method is used for the following purpose:

  1. People access the website with multiple URLs.
  2. It is used for moving sites to the new domain and make the transition as smooth as possible.
  3. It is also used for merging different websites.

302 redirect

This method is a temporary redirect method. It is mainly useful for SEO purposes. When the user has a temporary landing page, and the user plans to switch back to the main landing page after time.

Example

Redirect www.errorsea.com to http://demo.errorsea.com using this method.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.errorsea\.com$ [NC]
RewriteRule ^(.*)$ http://demo.errorsea.com/ [L,R=302]

This method is used for the following purpose:

  1. We can get client feedback on a new page without affecting site ranking.
  2. A/B testing of a web page for checking design or functionality.
  3. We can update a web page while providing users with a consistent experience.

303 redirect

This method uses the GET method. It informs the server to fetch the information on the server. 

Note: This method was used for preventing a form resubmission after the HTTP POST request.

Suppose the user bookmarks the page, then it doesn’t show the user data submitted. Instead, this method shows him/her a page saying, “your data submitted successfully.”

This method is used for the following purpose:

  1. Device targeting
  2. Tracking
  3. A/B testing
  4. Geotargeting
  5. Recurring temporary content
  6. Redirect with no effect to the presence in search results

307 redirect

This method is also a temporary redirect method like 303 redirects, but it is a non-cacheable method as default. 

We can use this method to temporarily redirect a new URL to preserve the original URL request’s request methods.

Note: If the redirect is permanent, then this method should be avoided.

It shouldn’t use with a PRG pattern.

308 redirect

This method is cacheable by default if the user doesn’t indicate it. This method can be used to handle the request method from the original request for the subsequent request.

For example, redirect of form action URLs using the POST method.

Note: This method is the wrong choice for all occasions in which catchability can leads the user to unexpected negative behavior.

This method is used for the following purpose:

  1. Moving a domain permanently
  2. Moving a document permanently
  3. Moving a very complex website containing many forms and using the POST method.
  4. Changing a site’s structure forever.
  5. Changing a site’s protocol permanently.
  6. When subsequent request requires the POST method

Read More: How to Redirect Page in PHP [All Methods]

Conclusion

In this article, we learned all types of HTACCESS redirect methods in great detail. 

Enjoy Programming 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *