Cloudbooklet
  • News
  • Artificial Intelligence
  • Applications
  • Linux
No Result
View All Result
Cloudbooklet
  • News
  • Artificial Intelligence
  • Applications
  • Linux
No Result
View All Result
Cloudbooklet
No Result
View All Result
Home Linux

How to Redirect using Htaccess – Common Rules for 301, 302, HTTPS

by Cloudbooklet
2 years ago
in Linux
How To Redirect Using Htaccess
ShareTweetSendShare
Readers like you help support Cloudbooklet. When you make a purchase using links on our site, we may earn an affiliate commission.

How to configure redirection using .htaccess file. Htaccess is file which is used to make changes to your web server configuration without editing the server configuration file. This .htaccess file will be placed in your website root directory. In this guide you are going to learn to how to configure some list of redirections which […]

ADVERTISEMENT

How to configure redirection using .htaccess file. Htaccess is file which is used to make changes to your web server configuration without editing the server configuration file. This .htaccess file will be placed in your website root directory.

In this guide you are going to learn to how to configure some list of redirections which is mostly needed for your SEO and ranking in search engines.

These are the list of redirections you are going to learn here.

ADVERTISEMENT

Table of Contents

  1. Redirect HTTP to HTTPS with www
  2. Redirect HTTP to HTTPS without www
  3. 3. Redirect one page to another page
  4. Redirect old domain to new domain
  5. Redirect old domain to new domain while preserving the path
  6. Redirect files location to new domain with same path
  7. Redirect except certain path
  8. Redirect subdomain to subfolder

Note: The below redirections use 301 permanent redirection, so you if need to can try the rules by using 302 temporary redirection instead and test them.

You might also like

&Quot; Systemd Service On Linux

How to Create a New systemd Service on Linux: A Step-by-Step Guide

3 months ago
List Groups In Linux

How to List Groups in Linux: A Guide for Beginners

3 months ago

Redirect HTTP to HTTPS with www

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]

Redirect HTTP to HTTPS without www

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

3. Redirect one page to another page

Syntax is

# Syntax
Redirect type(302/301) old_location new_location
# Redirect to a page
Redirect 301 /old_page https://www.domain.com/new_page

# Redirect to folder
Redirect 301 /old_page https://www.domain.com/folder/

Redirect old domain to new domain

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^(.*)$ "https\:\/\/new-domain\.com\/" [R=301,L]

Redirect old domain to new domain while preserving the path

RewriteEngine on
RewriteCond %{HTTP_HOST} ^olddomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
RewriteRule ^(.*)$ "https\:\/\/newdomain\.com\/$1" [R=301,L]

Redirect files location to new domain with same path

RewriteRule ^.*\.(pdf|doc)$ https://newdomain.com%{REQUEST_URI} [R=301,L]

Redirect except certain path

For example if you wish to redirect all your website frontend to a new domain except your WordPress wp-admin pages, then you need to use the following rules before the redirection rules.

ADVERTISEMENT
RewriteCond %{REQUEST_URI} !^(.*)?wp-login\.php(.*)$
RewriteCond %{REQUEST_URI} !^(.*)?wp-admin$
RewriteCond %{REQUEST_URI} !^/(wp-includes/.*|wp-admin/.*|wp-content/.*)$

Redirect subdomain to subfolder

This mainly used for redirecting a subdomain based site to a subfolder based site.

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI}/ subfolder
RewriteRule ^(.*) https://domain.com/%{REQUEST_URI} [R=301,NC]
RewriteRule ^(.*) https://domain.com/blog/%{REQUEST_URI} [R=301,NC]
ADVERTISEMENT
Tags: Apachehtaccessredirection
Share2Tweet1SendShare
Cloudbooklet

Cloudbooklet

Comments 1

  1. Avatar Of Ashikur Rahman ASHIKUR RAHMAN says:
    2 months ago

    hi, which code okay to use?

    redirect from several specific subdomains then you can modify the above rule:

    RewriteCond %{HTTP_HOST} ^dev\.example\.com [NC,OR]
    RewriteCond %{HTTP_HOST} ^dev0\.example\.com [NC,OR]
    RewriteCond %{HTTP_HOST} ^dev1\.example\.com [NC]
    RewriteRule ^directory/ https://example.com%{REQUEST_URI} [R=302,L]

    or this one:

    RewriteCond %{HTTP_HOST} ^dev\.example\.com [NC,OR]
    RewriteCond %{HTTP_HOST} ^dev0\.example\.com [NC,OR]
    RewriteCond %{HTTP_HOST} ^dev1\.example\.com [NC]
    RewriteRule ^directory/ https://example.com%{REQUEST_URI} [R=301,L]

    which one to use?

    Reply

Leave a Reply Cancel reply

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

Related Posts

Hostname In Linux

How to Modify the Hostname in Linux

3 months ago
Linux Systems

Linux systems Hacked with OpenSSH Malware

3 months ago
Install Iptables On Ubuntu

How to Install Iptables on Linux

3 months ago
Open Port In Linux

How to Open Port in Linux: Simple Step-by-Step Guide

3 months ago

Follow Us

Trending Articles

Ai Trainer

How to Become an AI Trainer: Skills, Salary, and Career Opportunities

September 15, 2023

10 Best AI Prompts for Writers to Improve Website SEO

Microsoft Editor vs Grammarly: Which is the Best Grammar Checker?

10 Best AI Song Generator in 2023 (Free and Paid)

How to Create and Customize Stunning Contact Poster on iPhone

10 Best Minecraft Server Hosting Providers in 2023

Popular Articles

Anonymous Email Account

Top 8 Anonymous Email Account Providers to Protect Your Online Identity

August 31, 2023

4 Ways to Reset Snapchat Password

ImgCreator AI: Free AI Image Generator by ZMO

10 Best Free Naked AI Generator: Make Realistic Naked Photos

12 Best AI Interior Design Platforms for 2023 [Free and Paid]

Top 7 Free Dating Sites for Men in 2023

Subscribe Now

loader

Subscribe to our mailing list to receives daily updates!

Email Address*

Name

Cloudbooklet Logo

Welcome to our technology blog, where we explore the latest advancements in the field of artificial intelligence (AI) and how they are revolutionizing cloud computing. In this blog, we dive into the powerful capabilities of cloud platforms like Google Cloud Platform (GCP), Amazon Web Services (AWS), and Microsoft Azure, and how they are accelerating the adoption and deployment of AI solutions across various industries. Join us on this exciting journey as we explore the endless possibilities of AI and cloud computing.

  • About
  • Contact
  • Disclaimer
  • Privacy Policy

Cloudbooklet © 2023 All rights reserved.

No Result
View All Result
  • News
  • Artificial Intelligence
  • Applications
  • Linux

Cloudbooklet © 2023 All rights reserved.