title: Rewrite sub folder dynamically with country code in WordPress using PHP
body: <p>I know this question has been asked so many times but I didn't find any working solution or example which I can use to fix my problem.</p>
  
  <p>I have been working on a client site. There are two similar sites, one for their own country and second for other country's visitors.</p>
  
  <p>Their main site hosted in the root of the server and second site hosted in the subfolder.</p>
  
  <p>Now what I want is a dynamic URL rewrite for the second site which is hosted into a subfolder with the country code of the visiting user.</p>
  
  <p>For e.g.</p>
  
  <p><code>http://example.com</code>
  <br>
  <code>http://example.com/subfolder/</code></p>
  
  <p>are the URLs.</p>
  
  <p>I want this <code>http://example.com/subfolder/</code> to be changed into this <code>http://example.com/country_code/</code> where <code>country_code</code> is visitor country code in ISO format getting through PHP function.</p>
  
  <p>So if the user is from <strong>United States</strong> the <code>subfolder</code> must be changed into <code>us</code>, the new URL should be now <code>http://example.com/us/</code>.</p>
  
  <p>I want this to work for all types of pages, whether its a page, post, category, tag or author page.</p>
  
  <p>So again, <code>http://example.com/subfolder/any-type-of-url/</code> => <code>http://example.com/country_code/any-type-of-url/</code></p>
  
  <p>Remember <code>country_code</code> is user/visitor country code in ISO format.</p>
  
  <p>Let me know if someone needs more information on this. Thanks in Advance.</p>
  
  <p>PS: I tried to achieve this using <code>add_rewrite_rule()</code> function available in WP.</p>