.htaccess 301 redirect

What is .htaccess?

.htaccess is a configuration file used by Apache Web Server Software. Through .htaccess file, you can change the configuration of Apache Web Server to enable or disable rewrite rules and some functionality the Apache Web Server has to offer. Note .htaccess files must be uploaded in ASCII mode (binary mode is not supported). Some basic functionality that can be controlled via Apache .htaccess is:

  1. Setting 404 file not found file when this error occurs.
  2. Password protection
  3. Image hotlink prevention.
  4. Most import is URL rewriting (which is the main discussions of today’s article)

Today’s tutorial is mainly about .htaccess 301 redirect, which is just another example of using rewriting capabilities of Apache Web Server through .htaccess file. Also note that with our OpenLiteSpeed server, you can use super fast OpenLiteSpeed Server along with Apache on the back end, thus you will get a speed of OpenLiteSpeed and all the goodies of Apache Web Server such as .htaccess compatibility. This set up also works with LSCache WordPress plugin which means you can also get full benefits of OpenLiteSpeed full page caching.


.htaccess redirect 301

The .htaccess file allows the user to move from one document on the website to another through rewrite directives. 301 redirect allows webmasters to permanently move users to new URL while replacing the old URL and tell the Search Engines that the page has moved to another new URL so the new page is properly indexed in google.

If you have root access to your VPS you can also set rewrite rules in your main Apache configuration file, which will increase your website speed (make sure to set AllowOverride None, which tells apache not to look for .htaccess files in every directory it serves content from, thus improving overall speed). However, if you don’t have root access (in case of shared hosting) you will have to use a rewrite file (.htaccess) at a directory level.

Common uses of a 301 redirect:

  1. After moving your site to a new domain, you can use 301 redirect on the old domain to send visitors to the new site.
  2. After moving old site pages to a new site structure, use 301 redirects to send users to the new pages.
  3. After combining two sites, use 301 redirects to ensure visitors go to the new pages or the right pages.

Single URL redirect:

Allow the user to redirect permanently from old_page.html to new_page.html. For the redirection, you have to add the following code in your .htaccess file:

Redirect 301 /old_page.html http://www.yourdomain.com/new_page.html

Entire domain redirect:

.htaccess 301 redirect also allows the user to permanent redirect from all domain pages to your new domain. For the redirection, you have to add the following code in your .htaccess file:

Redirect 301 / http://www.newdomain.com/

If doing this is too much for you, you can sign up with us and let our experts do this for you. Contact us to get started.

HTTP to HTTPS redirect:

Redirect 301 allows the user to redirect the website from HTTP to HTTPS secure mode. For the redirection, you have to add the following code in your .htaccess file:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

HTTP TO HTTPS redirect in CyberPanel

CyberPanel gives you one-click solution for HTTP -> HTTPS redirection.

Wesbites -> List Wesbites -> Manage

 

 

  1. Once on the Manage page of the website click Rewrite Rules.
  2. From drop-down select Force HTTP -> HTTPS (Rewrite rules will be populated automatically).
  3. Click Save Rewrite Rules.

If doing this is too much for you, you can sign up with us and let our experts do this for you. Contact us to get started.


How to 301 Redirect WordPress Content Using .htaccess in CyberPanel

A 301 redirect is a permanent redirect and it tells search engines, including Google to remove the original URL from their index and replace it with the one you’re redirecting to. So let’s put that code from above into your .htaccess file and redirect some pages.

First, log into the CyberPanel dashboard. Navigate to Websites -> List Websites then try to find the Manage button or just click on the link /home/example.cyberhosting.org/public_html this will file manager and navigate you to the document root of the website.

You can also log into your website root using an FTP account if you are more comfortable with that.

In the Document Root of WordPress, find the .htaccess file and edit it. Copy Paste the redirect 301 code in a .htaccess file and click on save changes.

Code:

Redirect 301 /old_page/ http://domain.com/new_page/

You can test the website URL redirection immediately after saving the changes in the .htaccess file. If you are not redirected then something went wrong. That is how you redirect a page or redirect URL using .htaccess.


What is Error Document in .htaccess

Apart from using .htaccess file for rewriting you can also set up custom error pages in case of 404 or other errors. Making a Custom error page in your website is very helpful, it allows a user to see the custom user-friendly error message in case your application ran into some problem or user tried to access the page which does not exists (404 error). Let’s suppose you create the custom error page for your website and now you want to activate in the .htaccess file. Move to your root directly of your web site and find the .htaccess file. If you find the file you need to edit it, if there is no such file you can create your own .htaccess file.

Add the following lines in your .htaccess file:

ErrorDocument 404 /error404.html

After ErrorDocument specifies the error code, followed by a space, and then the path and filename of the .html customized error file you would like to be displayed when the specified error is generated.

If doing this is too much for you, you can sign up with us and let our experts do this for you. Contact us to get started.


CyberPanel and .htaccess

If you are using CyberPanel with OpenLiteSpeed you need to be extra careful because OpenLiteSpeed does not support all the directives of Apache Web Server, unless you are using our LiteSpeed Enterprise Web Server which is fully compatible with Apache that means you get all the features of Apache Web Server and speed of LiteSpeed.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.