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. Technical SEO Issues
    4. How to force a trailing slash after the domain name

    How to force a trailing slash after the domain name

    Technical SEO Issues
    11 2 4.4k
    • 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.
    • JollyBoy
      JollyBoy last edited by

      My campaign analysis is predictably listing domain.com and domain.com/ as repeated content. I've searched and searched but cannot find a way to force a trailing slash on the end of the domain name unless there's a file or directory after it..

      Is there a way to accomplish this using .htaccess

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

        Hi Clive.

        Yes, you can easily do this with an .htaccess file, here is the code:

        RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} !(.*)/$ RewriteRule ^(.*)$ http://domain.com/$1/ [L,R=301]

        Just replace "domain.com" with your proper url for your site. This should be all that is needed.

        Hope this helps!

        JollyBoy 1 Reply Last reply Reply Quote 0
        • JollyBoy
          JollyBoy @BailHotline last edited by

          Thanks for the reply, but this looks like all the other examples I've found. My .htaccess file looks like this :

          DirectoryIndex index.php

          RewriteEngine On
          RewriteCond %{REQUEST_FILENAME} !-f
          RewriteCond %{REQUEST_FILENAME} !-d
          RewriteRule ^(.*)$ $1.php [L,QSA]

          RewriteCond %{REQUEST_FILENAME} !-f
          RewriteCond %{REQUEST_URI} !(.)/$
          RewriteRule ^(.
          )$ http://domain.co.uk/$1/ [L,R=301]

          But I get the following redirects going on:

          domain.co.uk > domain.co.uk (ie nothing happens)

          domain.co.uk/ > domain.co.uk (ie slash is removed)

          domain.co.uk/page2 > domain.co.uk/page2 (ie nothing happens, but page loads)

          domain.co.uk/page2/ > Internal server error

          Any ideas?

          BailHotline JollyBoy 8 Replies Last reply Reply Quote 0
          • BailHotline
            BailHotline @JollyBoy last edited by

            Try just the following:

            Let me know if this works for you.

            RewriteEngine On
            RewriteBase /
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_URI} !index.php
            RewriteCond %{REQUEST_URI} !(.*)/$
            RewriteRule ^(.*)$ http://www.domain.com/$1/ [L,R=301]
            
            1 Reply Last reply Reply Quote 0
            • JollyBoy
              JollyBoy @JollyBoy last edited by

              Nope. Still no trailing slashes being added.

              1 Reply Last reply Reply Quote 0
              • BailHotline
                BailHotline @JollyBoy last edited by

                Is this the only thing you have in your htaccess file?

                if not, I would remove everything in the file and only have what i posted above, and let me know if it works.

                1 Reply Last reply Reply Quote 0
                • JollyBoy
                  JollyBoy @JollyBoy last edited by

                  That's right - nothing in there but the code you supplied.

                  1 Reply Last reply Reply Quote 0
                  • BailHotline
                    BailHotline @JollyBoy last edited by

                    Man, my mind is blown right now. I'm not giving up and hopefully someone else can chime in on this discussion and shed some light on this issue.

                    The code provided should have worked. Let me look into it some more. Also, if you don't mind what is the actual domain name?

                    1 Reply Last reply Reply Quote 0
                    • JollyBoy
                      JollyBoy @JollyBoy last edited by

                      Thanks for sticking with this. Rather than me share the domain, do you know of any example sites using your code (or similar) which add a trailing slash after the domain name? I'd like to rule out my browser stripping it out.

                      1 Reply Last reply Reply Quote 0
                      • BailHotline
                        BailHotline @JollyBoy last edited by

                        Test Site: soulgorithm.com

                        In the .htaccess file for this site:

                        Options +FollowSymlinks
                        RewriteEngine on
                        RewriteBase /
                        RewriteCond %{HTTP_HOST} ^www.soulgorithm.com [NC]
                        RewriteRule (.*) http://soulgorithm.com/$1 [L,R=301]

                        RewriteCond %{REQUEST_URI} (.)/$
                        RewriteCond %{REQUEST_FILENAME}.html -f
                        RewriteRule (.
                        )/$ $1.html [L]

                        RewriteCond %{REQUEST_URI} (.)/$
                        RewriteCond %{REQUEST_FILENAME}.php -f
                        RewriteRule (.
                        )/$ $1.php [L]

                        RewriteCond %{REQUEST_FILENAME} !-d
                        RewriteCond %{REQUEST_FILENAME}.html -f [OR]
                        RewriteCond %{REQUEST_FILENAME}.php -f
                        RewriteRule .* %{REQUEST_FILENAME}/ [R=301,L]

                        Which has the following effect:

                        soulgorithm.com > soulgorithm.com/

                        (slash is added, but only shows in IE and looks

                        likes its being stripped by Firefox but page

                        still loads fine)

                        soulgorithm.com/ > soulgorithm.com/
                           
                            (loads fine, but only shows in IE and looks

                        likes its being stripped by Firefox but page

                        still loads fine)

                        soulgorithm.com/test > soulgorithm.com/test/

                        (loads fine, slash even shows in FF)

                        soulgorithm.com/test/ > soulgorithm.com.com/test/

                        (loads fine)

                        soulgorithm.com/testdir > soulgorithm.com/testdir/

                        (loads fine, slash even shows in FF)

                        soulgorithm.com/testdir/ > soulgorithm.com.com/testdir/

                        (loads fine, slash even shows in FF)

                        Let me know if this is what you see. I feel likes its getting close to working.

                        1 Reply Last reply Reply Quote 2
                        • JollyBoy
                          JollyBoy @JollyBoy last edited by

                          I've gone with this .htaccess from your soulgorithm.com:

                          Options +FollowSymlinks
                          RewriteEngine on
                          RewriteBase /
                          RewriteCond %{HTTP_HOST} ^www.domain.co.uk [NC]
                          RewriteRule (.*) http://domain.co.uk/$1 [L,R=301]

                          RewriteCond %{REQUEST_URI} (.)/$
                          RewriteCond %{REQUEST_FILENAME}.php -f
                          RewriteRule (.
                          )/$ $1.php [L]

                          RewriteCond %{REQUEST_FILENAME} !-d
                          RewriteCond %{REQUEST_FILENAME}.php -f
                          RewriteRule .* %{REQUEST_FILENAME}/ [R=301,L]

                          and I'm now getting the results I'm after. I'm getting similar behaviour to you in Firefox and IE, which explains a lot. I really appreciate the length you've gone to to help me here, so big thank you!

                          1 Reply Last reply Reply Quote 0
                          • 1 / 1
                          • First post
                            Last post
                          • I want to shift my website to a new domain name, with my brand name. Would Lose rankings
                            effectdigital
                            effectdigital
                            1
                            3
                            66

                          • 301 Old Domain Name with relevant domain name
                            0
                            1
                            32

                          • Trailing Slashes on URLs
                            GastonRiera
                            GastonRiera
                            0
                            2
                            146

                          • URL Structure On Site - Currently it's domain/product-name NOT domain/category/product name is this bad?
                            RuthBurrReedy
                            RuthBurrReedy
                            0
                            4
                            196

                          • Trailing Slashes and SEO
                            max.favilli
                            max.favilli
                            0
                            8
                            634

                          • Domain Forwarding / Multiple Domain Names / or Rebuild Blogs on them
                            smarties954
                            smarties954
                            0
                            2
                            161

                          • Can I do a redirect to a new domain name only a couple of weeks after having redirected to another domain?
                            Francisco_Meza
                            Francisco_Meza
                            0
                            4
                            428

                          • How to 301 multiple domain names to a single domain
                            KeriMorgret
                            KeriMorgret
                            0
                            9
                            1.5k

                          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