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. SEO and Digital Marketing Forum
    2. Categories
    3. SEO Tactics
    4. International SEO
    5. Anyone know their stuff when it comes to Rewrite rules in Htaccess?
    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.

    Anyone know their stuff when it comes to Rewrite rules in Htaccess?

    International SEO
    2 2 2.7k
    • 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.
    • O2C
      O2C last edited by

      Hi All,

      I have the following code in one of our Htaccess files and I'm not entirely sure what its doing. Could anyone shed some light and maybe explain the process its going through? I know its something to do with redirecting the urls dependant on the browser language.

      RewriteCond %{REQUEST_URI} ^/(En|Es)$ [NC]

      RewriteRule ^(En|Es)/(.*)$ $2?lang=$1 [L,R=301]

      RewriteCond %{REQUEST_URI} !(.[a-zA-Z0-9]{1,5}|/)$

      RewriteRule ^(.*)$ $1/ [L,R=301]

      Thanks

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

        Hey David,

        Sorry for the delayed response on this.

        Before we get started, I should point out that htaccess's syntax is very particular and you should be extremely careful when messing with it. even a single space out of place can cause massive errors. If you're planning changes, please consult with a developer or three on your team!

        I think the best way to explain this is to go through exactly what the htaccess Rewrite calls are doing.

        RewriteCond %{REQUEST_URI} ^/(En|Es)$ [NC]

        RewriteCond - The condition of which a rewrite will take place
        %{REQUEST_URI} - The URI that is requested from the server (everything after the domain and TLD i.e. moz.com/community 's URI would be /community)
        ^ - Denotes the beginning of a regular expression (regex)
        / - literally just /
        (En|Es) - the '()' are simply a grouping and the '|' means OR. So this is saying En OR Es
        $ - Denotes the end of a regex
        [NC] - Means no case, so everything in this is not case sensitive

        So literally this is saying execute this rewrite when the requested URI (after the .com or whatever TLD you use) is either /En OR /Es then whatever, with no attention to case

        RewriteRule ^(En|Es)/(.*)$ $2?lang=$1 [L,R=301]

        RewriteRule - The executed rule when the aforementioned RewriteCond is met.
        ^ - Denotes the beginning of a regular expression (regex)
        (En|Es) - the '()' are simply a grouping and the '|' means OR. So this is saying En OR Es
        / - literally just /
        (.) - This is a wildcard. Once again the () is a grouping, but here the . means zero or more arbitrary characters
        $ - Denotes the end of a regex
        $2 - this is the second captured grouping in this line. Meaning whatever is defined within (.*), which is everything after En/ or Es/
        ?lang= - this is literally writing '?lang=' without the 's.
        $1 - this is the first captured grouping from this line. Meaning whichever En OR Es was captured will be written here.
        [L] - Tells the server to stop rewriting after the preceding directive (rule) is processed
        [R] - Instructs Apache to issue a redirect, causing the browser to request the rewritten URL
        [301] - Corresponds to a Moved Permanetly Header Code
        [L,R=301] - Combines all 3 of these into one.

        For this I think it's easiest to just use an example.
        moz.com/En/htaccess-is-fun will be our example
        Since this url passes the RewriteCond, it goes on to the RewriteRule where it finds En OR Es and stores that value as $1 (En) then takes whatever is left and stores it as $2 (htaccess-is-fun).
        It then writes htaccess-is-fun?lang=En and replaces the original selection (which is En/htaccess-is-fun) with the new rewrite making the result moz.com/htaccess-is-fun?lang=En . The new URL is served as a 301-ed redirect.

        RewriteCond %{REQUEST_URI} !(.[a-zA-Z0-9]{1,5}|/)$

        RewriteCond - The condition of which a rewrite will take place
        %{REQUEST_URI} - The URI that is requested from the server (everything after the domain and TLD i.e. moz.com/community 's URI would be /community)
        ! - declares negation. i.e. "!cheese" matches everything except "cheese"
        () - is again a grouping
        \ - escapes a special character. So "." means a literal dot.
        a-zA-Z0-9 - matches all lowercase letters, all uppcase letters, and all numbers
        {1,5} - matches one to five of the previous designation. Meaning that there can be any combination of a-z, A-Z, or 0-9 in a sequence of one to five. i.e. A2ps OR 12345 OR AbC etc.
        | - Means OR
        / - literally just /
        $ - Denotes the end of a regex

        RewriteRule ^(.*)$ $1/ [L,R=301]

        ^ - Denotes the beginning of a regular expression (regex)
        (.) - This is a wildcard. Once again the () is a grouping, but here the . means zero or more arbitrary characters
        $ - Denotes the end of a regex
        $1 - this is the first captured grouping from this line.
        / - literally just /
        [L] - Tells the server to stop rewriting after the preceding directive (rule) is processed
        [R] - Instructs Apache to issue a redirect, causing the browser to request the rewritten URL
        [301] - Corresponds to a Moved Permanetly Header Code
        [L,R=301] - Combines all 3 of these into one.

        So whenever the RewriteCond is met, this rule will select everything and then rewrite it as a 301 with a / trailing it.

        Hope this helps! Let me know if you have any other questions.

        Regards,
        Trenton

        1 Reply Last reply Reply Quote 1
        • 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


        Explore more categories

        • Moz Tools

          Chat with the community about the Moz tools.

          Getting Started
          Moz Pro
          Moz Local
          Moz Bar
          API
          What's New

        • SEO Tactics

          Discuss the SEO process with fellow marketers

          Content Development
          Competitive Research
          Keyword Research
          Link Building
          On-Page Optimization
          Technical SEO
          Reporting & Analytics
          Intermediate & Advanced SEO
          Image & Video Optimization
          International SEO
          Local SEO

        • Community

          Discuss industry events, jobs, and news!

          Moz Blog
          Moz News
          Industry News
          Jobs and Opportunities
          SEO Learn Center
          Whiteboard Friday

        • Digital Marketing

          Chat about tactics outside of SEO

          Affiliate Marketing
          Branding
          Conversion Rate Optimization
          Web Design
          Paid Search Marketing
          Social Media

        • Research & Trends

          Dive into research and trends in the search industry.

          SERP Trends
          Search Behavior
          Algorithm Updates
          White Hat / Black Hat SEO
          Other SEO Tools

        • Support

          Connect on product support and feature requests.

          Product Support
          Feature Requests
          Participate in User Research

        • See all categories

        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

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