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. International Issues
    4. Anyone know their stuff when it comes to Rewrite rules in Htaccess?

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

    International Issues
    2 2 1.0k
    • 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
        • Redirection Question - Can Anyone Help?
          PatrickDelehanty
          PatrickDelehanty
          0
          3
          64

        • My local traffic has increased but USA traffic decreased..I dont know why!!
          katemorris
          katemorris
          0
          9
          208

        • International advice.... can anyone help and check my site?
          edward-may
          edward-may
          0
          5
          141

        • Anyone knows/seeing this strange bump in traffic from Granada, Spain?
          Cyrus-Shepard
          Cyrus-Shepard
          0
          2
          721

        • HI, can anyone give me pointers on how to move an ecommerce site to China?
          0
          1
          62

        • Google adwords keyword tool - Can anyone recommend a free service that is similar?
          EGOL
          EGOL
          0
          3
          192

        • New website coming soon
          ThompsonPaul
          ThompsonPaul
          0
          3
          382

        • Russian SEO: Do you know some good sources with tips and news about Yandex?
          MaratM
          MaratM
          0
          2
          906

        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