undefined
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.

    NEW Keyword Suggestions by Topic
    Moz Pro

    NEW Keyword Suggestions by Topic

    Learn more
  • 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
12.1k
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 Aug 3, 2016, 11:39 AM

    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 Jul 18, 2020, 4:14 AM Jul 18, 2020, 4:14 AM

      #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
      • topic:timeago_earlier,4 years
      • NeatIT
        NeatIT @Tom-Anthony last edited by Aug 5, 2016, 1:21 PM Aug 5, 2016, 1:17 PM

        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 Aug 5, 2016, 5:06 AM Aug 5, 2016, 5:06 AM

          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 Aug 5, 2016, 1:17 PM Reply Quote 0
          • 1 / 1
          1 out of 4
          • First post
            1/4
            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

          • SeoBlogs61

            Moving site from www to non www and also hosting to vps what will be the effect?

            shared hosting to vps www to non www

            Hi SEO gurus,
            I am trying to move my site from shared hosting to VPS hosting and also moving from www to non www version.
            What is the best possible way to avoid any issue and without losing the backlinks.
            Is it good or bad to do? URL: https://buylikesservices.com/

            Intermediate & Advanced SEO | Dec 6, 2022, 2:36 AM | SeoBlogs61
            0
          • Davit1985

            My url disappeared from Google but Search Console shows indexed. This url has been indexed for more than a year. Please help!

            Super weird problem that I can't solve for last 5 hours. One of my urls: https://www.dcacar.com/lax-car-service.html Has been indexed for more than a year and also has an AMP version, few hours ago I realized that it had disappeared from serps. We were ranking on page 1 for several key terms. When I perform a search "site:dcacar.com " the url is no where to be found  on all 5 pages. But when I check my Google Console it shows as indexed I requested to index again but nothing changed. All other 50 or so urls are not effected at all, this is the only url that has gone missing can someone solve this mystery for me please. Thanks a lot in advance.

            Intermediate & Advanced SEO | Apr 8, 2019, 8:19 AM | Davit1985
            0
          • 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 | Feb 23, 2017, 12:19 PM | gozmoz
            0
          • ozil

            How to do Country specific indexing ?

            We are a business that operate in South East Asian countries and have medical professionals listed in Thailand, Philippines and Indonesia. When I go to Google Philippines and check I can see indexing of pages from all countries and no Philippines pages. Philippines is where we launched recently. How can I tell Google Philippines to give more priority to pages from Philippines and not from other countries Can someone help?

            Intermediate & Advanced SEO | Jul 21, 2015, 7:48 AM | ozil
            0
          • 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 | Sep 6, 2017, 3:07 PM | iam-sold
            0
          • bjs2010

            Limit on Google Removal Tool?

            I'm dealing with thousands of duplicate URL's caused by the CMS... So I am using some automation to get through them - What is the daily limit? weekly? monthly? Any ideas?? thanks, Ben

            Intermediate & Advanced SEO | Jun 8, 2014, 6:24 PM | bjs2010
            0
          • Leverage_Marketing

            Reducing Booking Engine Indexation

            Hi Mozzers, I am working on a site with a very useful room booking engine.  Helpful as it may be, all the variations (2 bedrooms, 3 bedrooms, room with a view, etc, etc,) are indexed by Google.  Section 13 on Search Pagination in Dr. Pete's great post on Panda http://www.seomoz.org/blog/duplicate-content-in-a-post-panda-world speaks to our issue, but I was wondering since 2 (!) years have gone by, if there are any additional solutions y'all might recommend.  We want to cut down on the duplicate titles and content and get the useful but not useful for SERPs online booking pages out of the index.  Any thoughts? Thanks for your help.

            Intermediate & Advanced SEO | Apr 15, 2013, 4:10 PM | Leverage_Marketing
            0
          • cprodigy29

            Where to link to HTML Sitemap?

            After searching this morning and finding unclear answers I decided to ask my SEOmoz friends a few questions. Should you have an HTML sitemap? If so, where should you link to the HTML sitemap from? Should you use a noindex, follow tag? Thank you

            Intermediate & Advanced SEO | Mar 21, 2012, 5:23 PM | cprodigy29
            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
          • 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 - 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.