The Moz Q&A Forum

    • Forum
    • Questions
    • My Q&A
    • Users
    • Ask the Community

    Welcome to the Q&A Forum

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

    1. SEO and Digital Marketing Q&A Forum
    2. Categories
    3. Intermediate & Advanced SEO
    4. Mass 301 redirect in htaccess

    Mass 301 redirect in htaccess

    Intermediate & Advanced SEO
    4 3 100
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as question
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • moon-boots
      moon-boots last edited by

      I use ScreamingFrog to generate sitemaps for my Magento 2 multistore, but I recently noticed two issues.

      1. Each category/page has two URLs. One with / and the end and one without.
      2. Every product has two URLs. One with /product-name and the other /shop/product-name.

      The URLs are canonicalised, but this is still a problem and I'm not sure exactly how to execute this in the htaccess file.

      So I need to:

      1. Remove all URLs without the / at the end and redirect them all to the URL with / at the end. Or vice versa.
      2. 301 redirect every single product (there are over 400) from shop/product-name to /product-name.

      How do I do this en mass in the htaccess file?

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

        1. So for the first one what you want to do is a redirect and add the trailing slash. The .htaccess lines that you want to put in for that are:

        RewriteCond %{REQUEST_URI} !(/$|.)
        RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]

        effectdigital moon-boots 2 Replies Last reply Reply Quote 0
        • effectdigital
          effectdigital @Martijn_Scheijbeler last edited by

          This is solid advice

          If OP has access to NginX redirects (usually requires shell access, and obviously NginX) which are generally deemed to be faster and more efficient than .htaccess rewrite rules, they could do something like

          rewrite ^([^.]*[^/])$ $1/ permanent;

          I got it from here: https://stackoverflow.com/questions/645853/add-slash-to-the-end-of-every-url-need-rewrite-rule-for-nginx

          "The Regular Expression translates to: rewrite all URIs without any '.' in them that don't end with a '/' to the URI + '/'"

          Not all sites run NginX, but many do

          1 Reply Last reply Reply Quote -1
          • moon-boots
            moon-boots @Martijn_Scheijbeler last edited by

            Hi Martijn,

            The solution didn't work, I'm not sure if there is a conflict here but this is what my htaccess currently looks like:

            RewriteEngine On
            RewriteCond %{HTTPS} off
            RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

            #RewriteCond %{HTTPS} !=on
            #RewriteRule ^(.)$ https://thespacecollective.com/$1 [L,R=301]
            #RewriteRule ^(.
            )$ https://www.thespacecollective.com/$1 [L,R=301]

            <ifmodule pagespeed_module="">ModPagespeed off</ifmodule>

            RewriteRule .* - [E=noabort:1]

            RewriteRule .* - [E=noconntimeout:1]

            <ifmodule mod_rewrite.c="">RewriteEngine On
            RewriteCond %{HTTP_HOST} ^interstellarstore.com$ [OR]
            RewriteCond %{HTTP_HOST} ^www.interstellarstore.com$</ifmodule>

            RewriteRule (.*)$ https://www.thespacecollective.com/$1 [R=301,L]

            1 Reply Last reply Reply Quote 0
            • 1 / 1
            • First post
              Last post
            • Htaccess or url rewrite module for Magento 301 redirects?
              DmitriiK
              DmitriiK
              0
              4
              657

            • Handling Multiple Domain 301 Redirects on Single htaccess file
              Andy.Drinkwater
              Andy.Drinkwater
              0
              2
              291

            • Mass 301 redirect from a sub-domain - using Joomla or htaccess
              JohnW-UK
              JohnW-UK
              0
              3
              674

            • 301 Redirect To Another 301 Redirect
              cbielich
              cbielich
              0
              5
              247

            • 301 redirect subdomain to path and 301 for popular pages
              Mike.Goracke
              Mike.Goracke
              0
              2
              187

            • 301 redirect via htaccess question
              Audiohype
              Audiohype
              0
              4
              404

            • 301 Redirect using rewrite rule in .htaccess
              AlanMosley
              AlanMosley
              0
              15
              1.4k

            • Multiple 301 redirects considered a redirection chain?
              jmueller
              jmueller
              1
              16
              5.0k

            Get started with Moz Pro!

            Unlock the power of advanced SEO tools and data-driven insights.

            Start my free trial
            Products
            • Moz Pro
            • Moz Local
            • Moz API
            • Moz Data
            • STAT
            • Product Updates
            Moz Solutions
            • SMB Solutions
            • Agency Solutions
            • Enterprise Solutions
            • Digital Marketers
            Free SEO Tools
            • Domain Authority Checker
            • Link Explorer
            • Keyword Explorer
            • Competitive Research
            • Brand Authority Checker
            • Local Citation Checker
            • MozBar Extension
            • MozCast
            Resources
            • Blog
            • SEO Learning Center
            • Help Hub
            • Beginner's Guide to SEO
            • How-to Guides
            • Moz Academy
            • API Docs
            About Moz
            • About
            • Team
            • Careers
            • Contact
            Why Moz
            • Case Studies
            • Testimonials
            Get Involved
            • Become an Affiliate
            • MozCon
            • Webinars
            • Practical Marketer Series
            • MozPod
            Connect with us

            Contact the Help team

            Join our newsletter
            Moz logo
            © 2021 - 2026 SEOMoz, Inc., a Ziff Davis company. All rights reserved. Moz is a registered trademark of SEOMoz, Inc.
            • Accessibility
            • Terms of Use
            • Privacy