Category: Codes

A Curated Collection of Problem Solving Codes for your Website, Application or general-purpose tasks like Command Lines, OS Commands, Terminal Tools etc. Mostly With Screenshots and examples on how these codes are helpful in making a long task short.

  • Excellent Tricks For Redirection A Codes Collection

    Ultimate Guide to URL Redirection: Code Snippets and Best Practices (2023 Updated)

    Excellent Tricks For Redirection : Redirecting URLs is a crucial task for website owners, whether you’re migrating to a new domain, restructuring your site, or ensuring a seamless user experience. This guide provides updated code snippets and best practices for implementing redirections using HTML, JavaScript, Apache, Nginx, and PHP. We’ve also added new options and enhancements to ensure your redirections are efficient, SEO-friendly, and user-centric.


    Why URL Redirection Matters

    URL redirection is essential for:

    • SEO Preservation: Maintaining search engine rankings when changing URLs.
    • User Experience: Ensuring visitors land on the correct page.
    • Traffic Management: Redirecting traffic from old pages to new ones.
    • Security: Protecting against phishing and broken links.

    HTML and JavaScript Redirection

    1. Simple HTML + JavaScript Redirect

    This method uses both HTML meta tags and JavaScript for immediate redirection.

    <!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>Redirecting to Netnaps.com</title>
        </head>
        <body>
            <!-- Fallback for Non-JavaScript Users -->
            If you are not redirected automatically, follow this <a href='https://netnaps.com'>link to NETNAPS</a>.
        </body>
    </html>

    2. JavaScript Fallback for Non-JavaScript Users

    This snippet ensures redirection even if JavaScript is disabled.

    <script>
        window.location.replace("https://netnaps.com");
    </script>
    
    <noscript>
        <a href="https://netnaps.com">Click here if you are not redirected automatically.</a>
    </noscript>

    3. Delayed Redirection with Meta Tag

    Use this for a delayed redirect (e.g., 5 seconds).

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

    4. INIT Function with HTML Redirection

    This method uses JavaScript’s onload event to trigger redirection.

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

    Apache Server .htaccess Redirections

    1. Permanent Redirect 301 (Non-www to www)

    Redirects non-www to www for better consistency.

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

    2. Redirect www to Non-www

    Redirects www to non-www for a cleaner URL structure.

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

    3. Redirect www Domain with Folder/Subdirectory

    Redirects a domain with a specific folder or subdirectory.

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

    4. Redirect Old Domain to New Domain with Query String

    Redirects an old domain to a new one while preserving the query string and path.

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

    5. Redirect Entire Website

    Redirects all pages from one domain to another.

    Redirect 301 / https://netnaps.com/

    6. Redirect a Specific Page

    Redirects a particular page to a new location.

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

    7. Redirect Site to a Folder/Directory

    Redirects the entire site to a subfolder.

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

    8. HTML to PHP File Redirection

    Redirects all .html files to their .php equivalents.

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

    9. Redirect Old Domain to New Domain

    Redirects all traffic from an old domain to a new one.

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

    Nginx Redirects

    1. Redirect All HTTP Requests to HTTPS

    Ensures all traffic is redirected to HTTPS.

    server {
      listen [::]:80;
      listen      80;
      server_name netnaps.com www.netnaps.com;
    
      # Redirect all non-https requests
      rewrite ^ https://$host$request_uri? permanent;
    }

    2. Alternative Nginx HTTPS Redirect

    Another method for redirecting HTTP to HTTPS.

    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

    1. Basic PHP Redirection

    Redirects users using PHP’s header function.

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

    2. Conditional PHP Redirection

    Redirects users based on specific conditions.

    <?php
        if ($_SERVER['HTTP_HOST'] == 'olddomain.com') {
            header("Location: https://netnaps.com");
            exit();
        }
    ?>

    Best Practices for URL Redirection

    1. Use 301 for Permanent Redirects: Ensures SEO value is passed to the new URL.
    2. Test Your Redirects: Use tools like Redirect Checker to verify functionality.
    3. Avoid Redirect Chains: Ensure direct redirection to the final destination.
    4. Update Internal Links: Replace old URLs with new ones to minimize reliance on redirects.
    5. Monitor Performance: Use tools like Google Search Console to track redirect errors.

    New Enhancements and Tools

    • Cloudflare Redirects: Use Cloudflare’s page rules for serverless redirection.
    • WordPress Plugins: Plugins like Redirection or Simple 301 Redirects simplify the process.
    • Edge Side Includes (ESI): For dynamic content redirection in CDN environments.
    • Automated Redirect Mapping: Tools like Screaming Frog can automate bulk redirects.

    Conclusion

    URL redirection is a powerful tool for maintaining SEO, improving user experience, and managing website changes. With the updated code snippets and best practices provided in this guide Excellent Tricks For Redirection, you can implement redirections efficiently and effectively. Whether you’re using HTML, JavaScript, Apache, Nginx, or PHP, these solutions will ensure your website runs smoothly.


    Have you implemented URL redirections on your website? Share your experience or ask questions in the comments below! For more advanced tips and tools, explore our website management resources.