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.

      NEW Keyword Suggestions by Topic
      Moz Pro

      NEW Keyword Suggestions by Topic

      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?

      Check yours now
    • 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
      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

      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. Intermediate & Advanced SEO
    4. 6 .htaccess Rewrites: Remove index.html, Remove .html, Force non-www, Force Trailing Slash

    Moz Q&A is closed.

    After more than 13 years, and tens of thousands of questions, Moz Q&A closed on 12th December 2024. Whilst we’re not completely removing the content - many posts will still be possible to view - we have locked both new posts and new replies. More details here.

    6 .htaccess Rewrites: Remove index.html, Remove .html, Force non-www, Force Trailing Slash

    Intermediate & Advanced SEO
    3
    4
    12079
    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.
    • NeatIT
      NeatIT last edited by

      i've to give some information about my website Environment

      1. i have static webpage in the root.

      2. Wordpress installed in sub-dictionary www.domain.com/blog/

      3. I have two .htaccess , one in the root and one in the wordpress
      folder.

      i want to

      • www to non on all URLs
      • Remove index.html from url
      • Remove all .html extension / Re-direct 301 to url
        without .html extension
      • Add trailing slash to the static webpages / Re-direct 301 from non-trailing slash
      • Force trailing slash to the Wordpress Webpages / Re-direct 301 from non-trailing slash

      Some examples

      domain.tld/index.html >> domain.tld/

      domain.tld/file.html >> domain.tld/file/

      domain.tld/file.html/ >> domain.tld/file/

      domain.tld/wordpress/post-name >> domain.tld/wordpress/post-name/

      My code in ROOT htaccess is

      <ifmodule mod_rewrite.c="">Options +FollowSymLinks -MultiViews

      RewriteEngine On
      RewriteBase /

      #removing trailing slash
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^(.*)/$ $1 [R=301,L]

      #www to non
      RewriteCond %{HTTP_HOST} ^www.(([a-z0-9_]+.)?domain.com)$ [NC]
      RewriteRule .? http://%1%{REQUEST_URI} [R=301,L]

      #html
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^([^.]+)$ $1.html [NC,L]

      #index redirect
      RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index.html\ HTTP/
      RewriteRule ^index.html$ http://domain.com/ [R=301,L]
      RewriteCond %{THE_REQUEST} .html
      RewriteRule ^(.*).html$ /$1 [R=301,L]</ifmodule>

      The above code do

      1. redirect www to non-www
      2. Remove trailing slash at the end (if exists)
      3. Remove index.html
      4. Remove all .html
      5. Redirect 301 to filename but doesn't add trailing slash at the end

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

        #index redirect
        RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index.html\ HTTP/
        RewriteRule ^index.html$ http://domain.com/ [R=301,L]
        RewriteCond %{THE_REQUEST} .html
        RewriteRule ^(.*).html$ /$1 [R=301,L]

        hi anyone please help I use this code but now getting 404 error. please help.

        i also remove this code again but still same issue.

        1 Reply Last reply Reply Quote 0
        • NeatIT
          NeatIT @Tom-Anthony last edited by

          Hi Tom,

          thanks for your reply.

          i have some problems

          the above code doesn't

          1 - Add trailing slash to the static webpages / Re-direct 301 from non-trailing slash

          so it should be http://ghadaalsaman.com/articles/ instead of http://ghadaalsaman.com/articles

          2 - Force trailing slash to the Wordpress Webpages / Re-direct 301 from non-trailing slash

          1 Reply Last reply Reply Quote 0
          • Tom-Anthony
            Tom-Anthony last edited by

            Hey NeatIT!

            I see you have a working solution there. Did you have a specific question about the setup?

            I did notice that your setup cane sometimes result in chaining 301 redirects, which is one area for possible improvement.

            Let me know how we can help! 🙂

            NeatIT 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

            • gozmoz

              Moving html site to wordpress and 301 redirect from index.htm to index.php or just www.example.com

              I found page duplicate content when using Moz crawl tool, see below. http://www.example.com
              Page Authority 40
              Linking Root Domains 31
              External Link Count 138
              Internal Link Count 18
              Status Code 200
              1 duplicate http://www.example.com/index.htm
              Page Authority 19
              Linking Root Domains 1
              External Link Count 0
              Internal Link Count 15
              Status Code 200
              1 duplicate I have recently transfered my old html site to wordpress.
              To keep the urls the same I am using a plugin which appends .htm at the end of each page. My old site home page was index.htm. I have created index.htm in wordpress as well but now there is a conflict of duplicate content. I am using latest post as my home page which is index.php Question 1.
              Should I also use redirect 301 im htaccess file to transfer index.htm page authority (19) to www.example.com If yes, do I use
              Redirect 301 /index.htm http://www.example.com/index.php
              or
              Redirect 301 /index.htm http://www.example.com Question 2
              Should I change my "Home" menu link to http://www.example.com instead of http://www.example.com/index.htm that would fix the duplicate content, as indx.htm does not exist anymore. Is there a better option? Thanks

              Intermediate & Advanced SEO | | gozmoz
              0
            • morg45454

              Robots.txt - Do I block Bots from crawling the non-www version if I use www.site.com ?

              my site uses is set up at http://www.site.com I have my site redirected from non- www to the www in htacess file. My question is... what should my robots.txt file look like for the non-www site? Do you block robots from crawling the site like this? Or do you leave it blank? User-agent: * Disallow: / Sitemap: http://www.morganlindsayphotography.com/sitemap.xml Sitemap: http://www.morganlindsayphotography.com/video-sitemap.xml

              Intermediate & Advanced SEO | | morg45454
              0
            • friendoffood

              Pages are Indexed but not Cached by Google. Why?

              Here's an example: I get a 404 error for this: http://webcache.googleusercontent.com/search?q=cache:http://www.qjamba.com/restaurants-coupons/ferguson/mo/all But a search for qjamba restaurant coupons gives a clear result as does this: site:http://www.qjamba.com/restaurants-coupons/ferguson/mo/all What is going on?  How can this page be indexed but not in the Google cache? I should make clear that the page is not showing up with any kind of error in webmaster tools, and Google has been crawling pages just fine.  This particular page was fetched by Google yesterday with no problems, and even crawled again twice today by Google  Yet, no cache.

              Intermediate & Advanced SEO | | friendoffood
              2
            • iam-sold

              Best way to remove full demo (staging server) website from Google index

              I've recently taken over an in-house role at a property auction company, they have a main site on the top-level domain (TLD) and 400+ agency sub domains! company.com agency1.company.com agency2.company.com... I recently found that the web development team have a demo domain per site, which is found on a subdomain of the original domain - mirroring the site. The problem is that they have all been found and indexed by Google: demo.company.com demo.agency1.company.com demo.agency2.company.com... Obviously this is a problem as it is duplicate content and so on, so my question is... what is the best way to remove the demo domain / sub domains from Google's index? We are taking action to add a noindex tag into the header (of all pages) on the individual domains but this isn't going to get it removed any time soon! Or is it? I was also going to add a robots.txt file into the root of each domain, just as a precaution! Within this file I had intended to disallow all. The final course of action (which I'm holding off in the hope someone comes up with a better solution) is to add each demo domain / sub domain into Google Webmaster and remove the URLs individually. Or would it be better to go down the canonical route?

              Intermediate & Advanced SEO | | iam-sold
              0
            • Tone_Agency

              Wordpress blog in a subdirectory not being indexed by Google

              HI MozzersIn my websites sitemap.xml, pages are listed, such as /blog/ and /blog/textile-fact-or-fiction-egyptian-cotton-explained/These pages are visible when you visit them in a browser and when you use the Google Webmaster tool - Fetch as Google to view them (see attachment), however they aren't being indexed in Google, not even the root directory for the blog (/blog/) is being indexed, and when we query:site: www.hilden.co.uk/blog/ It returns 0 results in Google.Also note that:The Wordpress installation is located at /blog/ which is a subdirectory of the main root directory which is managed by Magento. I'm wondering if this causing the problem.Any help on this would be greatly appreciated!AnthonyToTOHuj.png?1

              Intermediate & Advanced SEO | | Tone_Agency
              0
            • Martin_S

              How do you de-index and prevent indexation of a whole domain?

              I have parts of an online portal displaying in SERPs which it definitely shouldn't be. It's due to thoughtless developers but I need to have the whole portal's domain de-indexed and prevented from future indexing. I'm not too tech savvy but how is this achieved? No index? Robots? thanks

              Intermediate & Advanced SEO | | Martin_S
              0
            • Celts18

              How to deal with old, indexed hashbang URLs?

              I inherited a site that used to be in Flash and used hashbang URLs (i.e.  www.example.com/#!page-name-here).  We're now off of Flash and have a "normal" URL structure that looks something like this:  www.example.com/page-name-here Here's the problem:  Google still has thousands of the old hashbang (#!) URLs in its index.  These URLs still work because the web server doesn't actually read anything that comes after the hash.  So, when the web server sees this URL  www.example.com/#!page-name-here, it basically renders this page www.example.com/# while keeping the full URL structure intact  (www.example.com/#!page-name-here).  Hopefully, that makes sense.  So, in Google you'll see this URL indexed (www.example.com/#!page-name-here), but if you click it you essentially are taken to our homepage content (even though the URL isn't exactly the canonical homepage URL...which s/b www.example.com/). My big fear here is a duplicate content penalty for our homepage.  Essentially, I'm afraid that Google is seeing thousands of versions of our homepage.  Even though the hashbang URLs are different, the content (ie. title, meta descrip, page content) is exactly the same for all of them. Obviously, this is a typical SEO no-no.  And, I've recently seen the homepage drop like a rock for a search of our brand name which has ranked #1 for months.  Now, admittedly we've made a bunch of changes during this whole site migration, but this #! URL problem just bothers me. I think it could be a major cause of our homepage tanking for brand queries. So, why not just 301 redirect all of the #! URLs?  Well, the server won't accept traditional 301s for the #! URLs because the # seems to screw everything up (server doesn't acknowledge what comes after the #). I "think" our only option here is to try and add some 301 redirects via Javascript. Yeah, I know that spiders have a love/hate (well, mostly hate) relationship w/ Javascript, but I think that's our only resort.....unless, someone here has a better way? If you've dealt with hashbang URLs before, I'd LOVE to hear your advice on how to deal w/ this issue. Best, -G

              Intermediate & Advanced SEO | | Celts18
              0
            • MyNet

              How To 301 Redirect .html pages

              I need to redirect a page/URL that is purely .html to a new location.  I don't know how to do this.  All the redirects I can find are for server side code pages .php/.aspx etc.  From my understanding I can't put a server side redirect in a .html file.  I am hosting on a microsoft server, however the new page I am redirecting to is .php.  I am running some WordPress (.php) files on the server.  I need to make it redirect before the old page loads so visitors don't start reading something that is about to get redirected Can someone please help me?

              Intermediate & Advanced SEO | | MyNet
              0

            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
            Free SEO Tools
            • Domain Authority Checker
            • Link Explorer
            • Keyword Explorer
            • Competitive Research
            • Brand Authority 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 - 2025 SEOMoz, Inc., a Ziff Davis company. All rights reserved. Moz is a registered trademark of SEOMoz, Inc.
            • Accessibility
            • Terms of Use
            • Privacy

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