LinuxWeb Servers

.htaccess redirects on Apache & LiteSpeed Servers

On Apache and LiteSpeed servers, you can use a .htaccess file to configure redirects. You can redirect pages to different pages which is useful if you are changing the name of a page or you can redirect folders and even entire websites to new websites. Using redirects is SEO friendly so you can redirect pages without any worries you will lose important rankings in this guide, we will go through the different types of .htaccess redirects and how to implement them.

To access your .htaccess file login to your site by FTP and navigate to the public_html folder and you will see the .htaccess file at the top of the file list. Download it to your computer and open it up. You can also get to this file by using the file manager in cPanel. You will need to click the “settings” button on the top right then “Show hidden files”.

301 .htaccess redirects Apache Servers

301 redirects are the most common type of redirect and are perfect for protecting your SEO work. A 301 redirect tells search engines that the resource they are trying to access has been moved permanently but, here is the new location. If you have done a lot of SEO link building on a page and you want the work you have done to be carried over to the new page, a 301 redirect will do this. There are two main ways to achieve a 301 redirect.

Redirect a single page

Redirect 301 /redirect.html https://www.site.com/new-name.html

Redirect a whole website

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

Redirect a whole website to a folder

Redirect 301 / https://www.newdomain.com/newfolder

Redirect a folder to a new domain

Redirect 301 https://www.olddomain.com/folder https://www.newdomain.com/

301 .htaccess redirects On LiteSpeed Servers

Mod_Rewrite is a module that is installed on every Apache and LiteSpeed server. It aims to process URL manipulations. Mod_Rewrite is the standard way to redirect pages because it offers more flexibility and options over using a simple 301 redirect. When using Mod_Rewrite you use codes to tell search engines why you are performing the redirect. So, if you are implementing a 301 redirect to a new page you would use the code [R=301,L]. That tells search engines the redirect is a 301. The L means that’s the last rule in this set.

Be careful when adding rewrite rules to your NVMe VPS Servers .htaccess file. Entering the syntax wrong will cause internal server errors when visiting your website. Before uploading or saving your .htaccess file, use a tool to check the syntax of your file.

Redirect from old domain to new domain

RewriteEngine on
RewriteBase /
RewriteRule (.*) https://www.newdomain.com/$1 [R=301,L]

Redirect to www

RewriteEngine on
RewriteBase /
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]

Redirect site to a folder using www

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} newdomain.com [NC]
RewriteRule ^(.*)$ http://www.newdomain.com/directory/site.html [R=301,NC]

Redirect traffic to https://www

So, the below code should not be edited to redirect all your traffic to the https://www version of your site. So, the code should just be placed in the .htaccess file.

RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Those are the basic and most common redirects you will use with the .htaccess file but it is possible to do more advanced redirects like query strings if required.

Related Articles

Leave a Reply

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

Back to top button