Skip to content
    Moz logo Menu open Menu close
    • Products
      • Moz Pro
      • Moz Pro Home
      • Moz Local
      • Moz Local Home
      • STAT
      • Moz API
      • Moz API Home
      • Compare SEO Products
      • Moz Data
    • Free SEO Tools
      • Domain Analysis
      • Keyword Explorer
      • Link Explorer
      • Competitive Research
      • MozBar
      • More Free SEO Tools
    • Learn SEO
      • Beginner's Guide to SEO
      • SEO Learning Center
      • Moz Academy
      • SEO Q&A
      • Webinars, Whitepapers, & Guides
    • Blog
    • Why Moz
      • Agency Solutions
      • Enterprise Solutions
      • Small Business Solutions
      • Case Studies
      • The Moz Story
      • New Releases
    • Log in
    • Log out
    • Products
      • Moz Pro

        Your all-in-one suite of SEO essentials.

      • Moz Local

        Raise your local SEO visibility with complete local SEO management.

      • STAT

        SERP tracking and analytics for enterprise SEO experts.

      • Moz API

        Power your SEO with our index of over 44 trillion links.

      • Compare SEO Products

        See which Moz SEO solution best meets your business needs.

      • Moz Data

        Power your SEO strategy & AI models with custom data solutions.

      Discover top competitors’ winning content
      What's New

      Discover top competitors’ winning content

      Learn More
    • Free SEO Tools
      • Domain Analysis

        Get top competitive SEO metrics like DA, top pages and more.

      • Keyword Explorer

        Find traffic-driving keywords with our 1.25 billion+ keyword index.

      • Link Explorer

        Explore over 40 trillion links for powerful backlink data.

      • Competitive Research

        Uncover valuable insights on your organic search competitors.

      • MozBar

        See top SEO metrics for free as you browse the web.

      • More Free SEO Tools

        Explore all the free SEO tools Moz has to offer.

      What is your Brand Authority?
      Moz

      What is your Brand Authority?

      Take the quiz
    • Learn SEO
      • Beginner's Guide to SEO

        The #1 most popular introduction to SEO, trusted by millions.

      • SEO Learning Center

        Broaden your knowledge with SEO resources for all skill levels.

      • On-Demand Webinars

        Learn modern SEO best practices from industry experts.

      • How-To Guides

        Step-by-step guides to search success from the authority on SEO.

      • Moz Academy

        Upskill and get certified with on-demand courses & certifications.

      • SEO Q&A

        Insights & discussions from an SEO community of 500,000+.

      Unlock flexible pricing & new endpoints
      The Moz API

      Unlock flexible pricing & new endpoints

      Find your plan
    • Blog
    • Why Moz
      • Small Business Solutions

        Uncover insights to make smarter marketing decisions in less time.

      • Agency Solutions

        Earn & keep valuable clients with unparalleled data & insights.

      • Enterprise Solutions

        Gain a competitive edge in the ever-changing world of search.

      • The Moz Story

        Moz was the first & remains the most trusted SEO company.

      • Case Studies

        Explore how Moz drives ROI with a proven track record of success.

      • New Releases

        Get the scoop on the latest and greatest from Moz.

      Surface actionable competitive intel
      New Feature: Moz Pro

      Surface actionable competitive intel

      Learn More
    • Log in
      • Moz Pro
      • Moz Local
      • Moz Local Dashboard
      • Moz API
      • Moz API Dashboard
      • Moz Academy
    • Avatar
      • Moz Home
      • Notifications
      • Account & Billing
      • Manage Users
      • Community Profile
      • My Q&A
      • My Videos
      • Log Out

    The Moz Q&A Forum

    • Forum
    • Questions
    • Users
    • Ask the Community

    Welcome to the Q&A Forum

    Browse the forum for helpful insights and fresh discussions about all things SEO.

    1. Home
    2. SEO Tactics
    3. On-Page Optimization
    4. How do I carry out a redirect? Is there a code I need to use?

    How do I carry out a redirect? Is there a code I need to use?

    On-Page Optimization
    redirect redirect chain
    13
    14
    122
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as question
    Log in to reply
    This topic has been deleted. Only users with question management privileges can see it.
    • laurentjb
      laurentjb Subscriber last edited by

      How do I carry out a redirect? Is there a code I need to use? Thank you in advance.

      hughcollins Shahreez BacklinkDigital 3 Replies Last reply Reply Quote 0
      • meltingpointgoat
        meltingpointgoat last edited by

        Redirecting sounds simple, but it can be a lot of work, especially for SEO. You need to carefully map old URLs to new ones, or you risk losing traffic. Big companies often have dedicated teams managing this to ensure everything’s smooth. Implementing strategies like resource smoothing helps here - spreading out tasks over time so no one is overwhelmed and SEO remains on point (see here).

        For example, you could have one team member focus on identifying which URLs need redirecting while another handles updating the code over a week, instead of doing everything at once. This way, the workload stays manageable, and there’s less risk of making mistakes.

        When implementing redirects, always check for broken links and monitor the changes to ensure traffic isn’t dropping. And remember, a good redirect strategy is essential for maintaining SEO, so take your time and do it right to avoid losing valuable rankings.

        1 Reply Last reply Reply Quote 0
        • jhk2021
          jhk2021 last edited by

          To carry out a redirect, you can use the following code depending on the language or platform you're working with.
          In HTML (meta tag redirect):

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

          In JavaScript:

          window.location.href = "https://new-url.com";
          

          In PHP:

          header("Location: https://new-url.com");
          exit();
          

          Choose the appropriate method based on your environment.

          Thanks!
          JHK Infotech

          1 Reply Last reply Reply Quote 0
          • BacklinkDigital
            BacklinkDigital @laurentjb last edited by

            @laurentjb ... Depend on your goals, if you are using cloudflare, you can go to page rules and create 301 / 302 redirect easily

            1 Reply Last reply Reply Quote 0
            • BacklinkDigital
              BacklinkDigital last edited by

              @laurentjb

              Depend on your goals, if you are using cloudflare, you can go to page rules and create 301 / 302 redirect easily

              1 Reply Last reply Reply Quote 0
              • shob0000
                shob0000 last edited by

                @laurentjb said in How do I carry out a redirect? Is there a code I need to use?:

                How do I carry out a redirect? Is there a code I need to use? Thank you in advance.
                To carry out a redirect, you typically use a piece of code specific to the platform you're working on. For example, in HTML, you can use <meta http-equiv="refresh" content="0;url=http://example.com">. If you’re using server-side code, such as PHP, you can use header("Location: http://example.com");. Just like how you might tweak a Wendy’s Taco Salad recipe to fit your taste, adjusting redirect code ensures your users are sent to the right place. i have wendy's taco salads related site and same done for that website too

                1 Reply Last reply Reply Quote 0
                • shtefcs
                  shtefcs last edited by

                  If you are using free CloudFlare DNS and SSL, you can create 301 redirection easily with their tools.

                  1 Reply Last reply Reply Quote 0
                  • jahangir43
                    jahangir43 last edited by

                    Yes, i have done many site like Digitech Journals. To carry out a redirect, you need to use specific code depending on the type of redirect and the server environment you’re working with. The most common types of redirects are 301 (permanent) and 302 (temporary) redirects.

                    1. Using .htaccess (Apache Server)

                    If your website is hosted on an Apache server, you can use the .htaccess file to create redirects.

                    a. 301 Permanent Redirect:

                    Redirect 301 /old-page.html https://www.example.com/new-page.html
                    
                    • Explanation: This code permanently redirects old-page.html to new-page.html.

                    b. 302 Temporary Redirect:

                    Redirect 302 /old-page.html https://www.example.com/new-page.html
                    
                    • Explanation: This code temporarily redirects old-page.html to new-page.html.

                    c. Redirecting an Entire Domain:

                    Redirect 301 / https://www.newdomain.com/
                    
                    • Explanation: This code permanently redirects all pages from the old domain to the new domain.

                    2. Using PHP for Redirects

                    If you have access to your website’s PHP code, you can use the following PHP code for redirects.

                    a. 301 Permanent Redirect:

                    <?php
                    header("HTTP/1.1 301 Moved Permanently");
                    header("Location: https://www.example.com/new-page.html");
                    exit();
                    ?>
                    
                    • Explanation: This PHP code permanently redirects to new-page.html.

                    b. 302 Temporary Redirect:

                    <?php
                    header("Location: https://www.example.com/new-page.html");
                    exit();
                    ?>
                    
                    • Explanation: This PHP code temporarily redirects to new-page.html.

                    3. Using JavaScript (Client-Side Redirect)

                    You can use JavaScript for client-side redirects, though it's generally better to use server-side redirects for SEO purposes.

                    a. JavaScript Redirect:

                    <script type="text/javascript">
                    window.location.href = "https://www.example.com/new-page.html";
                    </script>
                    
                    • Explanation: This script redirects the user to new-page.html.

                    4. Using Nginx

                    If your website is hosted on an Nginx server, you can set up redirects in the Nginx configuration file.

                    a. 301 Permanent Redirect:

                    server {
                        listen 80;
                        server_name www.example.com;
                        return 301 https://www.example.com$request_uri;
                    }
                    
                    • Explanation: This code redirects all HTTP traffic to HTTPS with a permanent redirect.

                    b. Redirecting a Single Page:

                    location /old-page.html {
                        return 301 https://www.example.com/new-page.html;
                    }
                    
                    • Explanation: This code permanently redirects old-page.html to new-page.html.

                    5. HTML Meta Refresh (Not Recommended for SEO)

                    You can use a meta refresh tag in the HTML <head> section, but it's not ideal for SEO as it's not a true redirect.

                    <meta http-equiv="refresh" content="0; url=https://www.example.com/new-page.html">
                    
                    • Explanation: This code redirects after 0 seconds to new-page.html.

                    Choosing the Right Redirect

                    • 301 Redirect: Use when you want to permanently move a page or domain. This tells search engines to pass the SEO value from the old URL to the new one.
                    • 302 Redirect: Use when the move is temporary, and you plan to revert to the old URL.

                    Always test your redirects after implementation to ensure they work correctly and that search engines and users are directed to the intended destination.

                    1 Reply Last reply Reply Quote 0
                    • squidgame2024
                      squidgame2024 last edited by

                      Complete redirection process requires using 301 redirection
                      You can redirect your old domain to the new one via Cloudflare or by contacting your hosting manager

                      1 Reply Last reply Reply Quote 0
                      • dotemna
                        dotemna last edited by

                        It depends on which type of redirect you mean and what the purpose is. Here are two common scenarios:

                        For redirecting a single page:
                        You can use a 301 redirect in your .htaccess file (for Apache servers):

                        Redirect 301 /old-page.html https://www.yoursite.com/new-page.html
                        

                        For redirecting an entire domain:
                        To redirect all pages from an old domain to a new one, you can use this code in your .htaccess file:

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

                        Both methods use 301 (permanent) redirects, which is typically best for SEO. Always test your redirects thoroughly to ensure they work as intended across your site.

                        1 Reply Last reply Reply Quote 0
                        • Umairaalam786
                          Umairaalam786 last edited by

                          @laurentjb said in How do I carry out a redirect? Is there a code I need to use?:

                          How do I carry out a redirect? Is there a code I need to use?

                          To carry out a redirect, you typically need to use specific code or configurations depending on the type of redirect you want to implement and the server or platform you're working with. The most common types of redirects are 301 (permanent) and 302 (temporary).

                          But I Recommend you Use Plugins
                          The Best WP Plugin is ( Redirection )

                          You can use plugins like Redirection or Yoast SEO to manage redirects without needing to write code.

                          301 Redirect: Use when the original page has permanently moved to a new URL.
                          302 Redirect: Use when the move is temporary.

                          1 Reply Last reply Reply Quote 0
                          • Shahreez
                            Shahreez @laurentjb last edited by

                            @laurentjb are you talking about domain redirection ?

                            1 Reply Last reply Reply Quote 0
                            • hughcollins
                              hughcollins @laurentjb last edited by

                              @laurentjb Yes you can redirect 1 page to another using any redirection plugin.
                              You can either 301(Moved Permanently) or 302(Moved Temporary) the Url 1 to Url 2

                              1 Reply Last reply Reply Quote 0
                              • Hami913
                                Hami913 last edited by

                                @laurentjb said in How do I carry out a redirect? Is there a code I need to use?:

                                How do I carry out a redirect? Is there a code I need to use? Thank you in advance.

                                To set up a redirect, you typically need to use some code depending on your server environment. For example, if you’re using Apache, you can add a redirect rule to your .htaccess file. Here’s a basic example of a 301 redirect in an .htaccess file:

                                apache

                                Redirect 301 /old-page.html http://www.yoursite.com/new-page.html

                                If you're using Nginx, you would add a redirect rule to your server configuration file like this:

                                nginx

                                location /old-page {
                                return 301 http://www.yoursite.com/new-page;
                                }

                                For WordPress or other CMSs, there are plugins available that can help you manage redirects without needing to handle code directly.

                                I hope this helps! Let me know if you have any more questions. because te same issue a=occure with my site then i resolve it by using this method.

                                Thank you!

                                1 Reply Last reply Reply Quote 0
                                • 1 / 1
                                • First post
                                  Last post

                                Got a burning SEO question?

                                Subscribe to Moz Pro to gain full access to Q&A, answer questions, and ask your own.


                                Start my free trial


                                Browse Questions

                                Explore more categories

                                • Moz Tools

                                  Chat with the community about the Moz tools.

                                • SEO Tactics

                                  Discuss the SEO process with fellow marketers

                                • Community

                                  Discuss industry events, jobs, and news!

                                • Digital Marketing

                                  Chat about tactics outside of SEO

                                • Research & Trends

                                  Dive into research and trends in the search industry.

                                • Support

                                  Connect on product support and feature requests.

                                • See all categories

                                Related Questions

                                • bcuclassifieds

                                  How to handle SEO redirection after remove www

                                  www to non www redirect chain classified website

                                  Recently our https://bestclassifiedsusa.com free classifieds USA website we move from www to non www format. Now old urls still showing in Google SERP. When click they show 404 error pages due to removed old ads.
                                  We redirect www to non www version for main domain but still old cache pages affected.
                                  How to dynamically handle this case, all old www version ads pages auto redirect to relevant category pages?

                                  Technical SEO | | bcuclassifieds
                                  0
                                • rj_dale

                                  Unsolved Finding broken urls

                                  404 error redirect list of urls

                                  Hello, This feels like it should be straightforward but I'm not having much luck. I have a pdf url which is no longer in use and takes the user to a 404 page. I want to find all instances of when this pdf url is used on my site so I can update the copy and remove the link. Please note - a redirect is not the right solution for this issue. Hope you can help. Thanks, Mary

                                  Moz Bar | | rj_dale
                                  0
                                • Ant-Scarborough

                                  Preserving Authority of closed website

                                  authority redirect 302

                                  Hi, I have the opportunity to acquire a domain of a company that is closing in a similar industry who currently have a decent Authority - 51DA, 5,752 Linking Domains. I cannot do anything with the new business yet but if I acquire the domain the existing website will turn off. If I crawl the existing site and 302 redirect all pages to a holding page on said domain, will that preserve authority while I start the new business and build the new website and like-for-like pages?

                                  Technical SEO | | Ant-Scarborough
                                  0
                                • mrmagnecom

                                  Is this method of redirecting of 404 okay?

                                  Hi all, This is my first post at this forum 🙂 I'am running a Opencart shop with a SEO extension installed. This extension has a redirect module which works like this. When visiting the site on 404 url (and if there is setup a redirect for that url). It serves the correct content but on the 404 url with the correct url set in rel canonical. For example: Try visit this url: 
                                  https://makitech.no/ventilasjon/ventilasjons-pakketilbud
                                  Here i have setup a redirect to the correct content, which is actually this url https://makitech.no/pakketilbud I am used to use a 301 header redirect so the user get redirected to the correct url. So my question is this. Is it a okay practice to serve the content on the 404 url (it responds 200) when the correct url is set in canonical? Any thoughts?

                                  On-Page Optimization | | mrmagnecom
                                  0
                                • xdunningx

                                  Using Bold text for keywords

                                  Hello I am updating an old e-commerce website of mine and many keywords are in bold - shall I remove the bold tag or keep them there? This is for SEO.

                                  On-Page Optimization | | xdunningx
                                  0
                                • marketing_zoovy.com

                                  Using Escaped Fragments with SEO

                                  Our e-commerce platform is in the process of changing to what we call app based stores (essentially running in a browser as single page web-app)  With these new stores they are being built in HTML 5 and using escaped fragments.
                                  Currently merchants are usually running 2 stores until we launch to app site at 100%.  My questions are really concerning the app stores which right now show on a subdomain but will essentially take over the primary domain.   Here is an example:
                                  app.tikimater.com and app.sportsworld.com Since I am not a developer, I'm really having a hard time understanding the escaped fragments. I'm using this but https://developers.google.com/webmasters/ajax-crawling/docs/getting-started I'm not sure what my actual urls should look like and what the canonical should be set to. Right now they have been removed but previously they had  http:app.tikimaster.com#!v=1  Also, and how I should be setting up my meta information for Google so 1) pages are indexed timely 2) pages are indexed with the correct information.  I am still setting the meta titles and descriptions but in some instances Google uses other info. With the new platform we are moving away from on page content (written paragraphs) but category pages would have related products embedded.  Should I still be pushing to have some type of intro text, since it would solely be for SEO and not the shoppers experience. All product pages have content (product description etc) Thank you for any advice

                                  On-Page Optimization | | marketing_zoovy.com
                                  0
                                • nagar

                                  Sitemap error is reported when using a sitemap-index generated by Yoast

                                  I've installed the Yoast SEO Plugin for wordpress and I've setup the sitemaps using it. I saw the tool has generated the Sitemap index file http://www.phraseexpander.com/sitemap_index.xml with different indexes for posts and pages I've submitted that to google and it's indexed. When I use Seoquake to check my website, I see that it says that the sitemap is missing (in fact http://www.phraseexpander.com/sitemap.xml) is returning 404. Shall I fix that? Shall I do a 301 redirect in my .htaccess file to http://www.phraseexpander.com/sitemap_index.xml Thanks.

                                  On-Page Optimization | | nagar
                                  0
                                • clickit2getwithit

                                  Temperary redirects

                                  I have a problem with temp redirects, I have a dating site and the redirects happen when guests on the site are prompted to purchase a subscription while trying gain access to the sites contents. I was told to change them to 301 redirects but don't know how. any help, thanks guys.

                                  On-Page Optimization | | clickit2getwithit
                                  0
                                Moz logo
                                • Contact
                                • Community
                                • Free Trial
                                • Terms & Privacy
                                • Accessibility
                                • Jobs
                                • Help
                                • News & Press
                                • MozCon
                                © 2021 - 2024 SEOMoz, Inc., a Ziff Davis company. All rights reserved. Moz is a registered trademark of SEOMoz, Inc.

                                Looks like your connection to Moz was lost, please wait while we try to reconnect.