Categories
Codes

Excellent Tricks For Redirection A Codes Collection

Find below code snippets for Redirection, Change your domain name accordingly

HTML Redirect / Javascript

Table of Contents

Simple Html + Javascript Redirect

<!DOCTYPE HTML>
<html lang="en-US">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="refresh" content="0; url=https://netnaps.com">
        <script type="text/javascript">
            window.location.href = "https://netnaps.com"
        </script>
        <title>Redirect Me to Netnaps.com</title>
    </head>
    <body>
        <!-- Mention to Redirect, If Not Automatically -->
        If you are not redirected automatically, follow this <a href='https://netnaps.com'>link to NETNAPS</a>.
    </body>
</html>

FallBack – Redirect if Javascript is not Allowed

<script>
    window.location.replace("https://netnaps.com");
</script>

<noscript>
    <a href="https://netnaps.com">Click here if you are not redirected automatically.</a>
</noscript>

Redirection With Meta Tag in Delayed Seconds

This is 5 seconds Delayed delay as you like

<meta http-equiv="refresh" content="5; url=https://netnaps.com/" />

INIT Function With HTML Redirection

<!DOCTYPE html>
<html>
    <head>
        <title>Example</title>
        <script>
            function init()
            {
               window.location.href = "www.netnaps.com";
            }
        </script>
    </head>

    <body onload="init()">
    </body>
</html>

Apache Server .htaccess Redirections

Permanent Redirect 301 Non-www to www

#redirect non-www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

Redirecting permanently www to non-www domain

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

Redirecting a www domain with folder or sub-directory permanent 301

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} netnaps.com [NC]
RewriteRule ^(.*)$ https://netnaps.com/directory/index.html [R=301,NC

Redirect an old domain to the new one with a full query string and path

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*) https://netnaps.com%{REQUEST_URI} [R=302,NC

Redirect The Entire Web Site

Redirect 301 / https://netnaps.com/

Redirecting a particular page

Redirect 301 /index.php https://netnaps.com/blog

Redirect the Site to a Folder / Directory

Redirect 301 / http://www.domain.com/subfolder/

HTML to PHP File Redirection

RedirectMatch 301 (.*)\.html$ https://netnaps.com$1.php

Redirecting an Old Domain to A New Domain

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

Nginx Redirects

Nginx All HTTP Requests to HTTPS

Use anyone both worked for me, If you use 443 Port use First One

server {
  listen [::]:80;
  listen      80;
  server_name netnaps.com www.netnaps.com;

  # Redirect all non-https requests
  rewrite ^ https://$host$request_uri? permanent;
}


server {
  listen              80;
  listen              [::]:80;
  server_name         netnaps.com www.netnaps.com;
  location '/var/www/netnaps' {
  default_type "text/plain";
  root        /tmp/dir;
  }

  location / {
    return              301 https://$server_name$request_uri;
  }
}

PHP Redirects

<?php
    Header("HTTP/1.1 301 Moved Permanently");
    Header("Location: https://netnaps.com");
?>

By Rahul Kharbanda

Hi,
I am Rahul Kharbanda from India. I hope you like all the content I made. Welcome to comment & connect.
I am on Quora , Github , Twitter

Leave a Reply

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