.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 todays article)


Todays 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 managed vps service, you can use super fast OpenLiteSpeed Server along with Apache on back end, thus you will get speed of OpenLiteSpeed and all the goodies of Apache Web Server such as .htaccess compabitibility. 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 servers 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 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


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 /


If doing this is too much for you, you can sign up with us and let our experts do this for you. We offer 3 days free trial no credit card required start free with our managed vps now.


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 Forcee 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. We offer 3 days free trial no credit card required start free with our managed vps now.

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 CyberPanel dashboard. Navigate to Websites -> List Websites then try to find Manage button or just click on the link /home/ this will file manager and navigate you to the document root of the website.


You can also log into your website root using 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.


Redirect 301 /old_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 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. We offer 3 days free trial no credit card required start free with our managed vps now.

CyberPanel and .htaccess


Since we provide our managed services on CyberPanel, you need to be extra careful if you are only using CyberPanel with OpenLiteSpeed, because OpenLiteSpeed does not support all the directives of Apache Web Server, unless you are using our managed vps service where you can use Apache behind OpenLiteSpeed which means you get all the features of Apache Web Server and speed of OpenLiteSpeed.


Switch Sites Between Pure OpenLiteSpeed and Apache + OpenLiteSpeed

In our privious documented we’ve discussed how you can create a website that use Apache as back end server. You can read about it here. If you want to learn more about this feature you can do that here. But let say if you have initially created website without using Apache as back end server, what can be done then? Answer is very simple, you can use Apache Manager to switch to Apache plus OpenLiteSpeed or vise versa at any point.

Switch to Apache + OpenLiteSpeed


Nagivate to Services -> Services -> Click Manage on Related Service -> Click Manage CyberPanel


There you will see list of websites, click on Launch Icon for the website you want to perform switch on. Then you will land on following page:



From the left menu under Apache Manager Click Switch. You will then land on page where you can switch to Apache + OpenLiteSpeed or pure OpenLiteSpeed (if site is on Apache + OpenLiteSpeed) with one click.



Here just select the desired PHP Version and click the switch button, you can see that button says Switch to pure OpenLiteSpeed as this site was originally created with Apache as back end.

How to create Website that use OpenLiteSpeed + Apache

We’ve discussed about Apache being used pararrel with OpenLiteSpeed in our privious doucment here. In the privious document we’ve discussed about what it is and its benefits. In this document we will learn how we can create a website that use OpenLiteSpeed + Apache.

Before moving forward you need to order a VPS through our platform and make sure it is already up and running, you can follow our order guide for more details regarding this.

Creating Website using Apache as Back end




Go to Services -> Services -> Click Manage, you will see a page where you can do basic management of your managed CyberPanel VPS, it will look something like:




Once there click Manage CyberPanel, it will take you the to page where you can manage various aspects of your server including creating a website. From top right corner click Create Website, and you will land on a page that will look like:



Here you can create website, fill all the details and make sure you check Apache as Back end. If this is your first website where you selected Apache it will then take some time to install Apache and PHP-FPM. Stay calm while the sites get created.


Please note that if your server have LiteSpeed Enterprise checking Apache as Back end will not make any difference because LiteSpeed Enterprise already offer all the features that Apache has to offer.



OpenLiteSpeed + Apache on CyberHosting Platform

At CyberHosting we use CyberPanel. We have a product line called CyberPanel VPS, this product line make use of OpenLiteSpeed web server. OpenLiteSpeed is fast and Event driven web server and have intelligent cache module that can help you achieve much better results when compared to NGINX or Apache. You can read this post as to why people start to use OpenLiteSpeed server.


However OpenLiteSpeed does not fully support .htaccess rules, also there is no support for custom php.ini files in OpenLiteSpeed.


Thus at CyberHosting we developed a solution where you can use Apache behind OpenLiteSpeed.

How that works?


On front end still you use OpenLiteSpeed, on backend Apache is configured. Thus you can still benefit from OpenLiteSpeed cache module and get similar speed benefits plus full support of .htaccess rules and custom php.ini files.


You can set inidividual websites on your server to either use OpenLiteSpeed purely or use a mix of Apache + OpenLiteSpeed.


You can also convert later between OpenLiteSpeed and Apache setup at any time, in our later tutorials we will learn how we can make use of this feature.


Learn How to create Website that use OpenLiteSpeed + Apache