• I have been working on a client site. There are two similar sites, one for their own country and second for other countries visitors.

    Their main site hosted in the root of server and second site hosted in the sub folder.

    Now what I want is dynamic url rewrite for second site which is hosted into sub folder with the country code of the visiting user.

    For e.g.

    https://example.com
    <br>
    https://example.com/subfolder/

    are the urls.

    I want this https://example.com/subfolder/ to be changed into this https://example.com/country_code/ where country_code is visitor country code in ISO format getting through php function.

    So if user is from United States the subfolder must be changed into us, the new url should be now https://example.com/us/.

    I want this to work for all type of pages, whether its a page, post, category, tag or author page.

    So again, https://example.com/subfolder/any-type-of-url/ => https://example.com/country_code/any-type-of-url/

    Remember country_code is user/visitor country code in ISO format.

    Let me know if someone needs more information on this. Thanks in Advance.

    PS: I tried to achieve this using add_rewrite_rule() function available in WP.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    Rewrite rules would be the solution. These rules need some bit of consistency in order to work properly, everything cannot be variable or WP would not know what rule to apply. This is why permalinks beyond the default structure have bases — tag, category, etc. The base presence unequivocally means the associated term is for a specific rule. Let’s say “co” would be the base. Permalinks could be /co/us/post-in-us-english/ or even /co-us/post-in-us-english/. The rewrite rule regexp will pick up on the “co” base and know this is the correct rule to apply. If there were always a country code as the first term in every link without fail, that is also consistency enough for rewrite rules.

    OTOH, you might be able to not rewrite at all, rather just extract the term from the request URI. If the country codes do not match up with anything WP knows about and there are no rewrite rules that match, WP will just ignore the term and fetch the post related to the slug that’s also in the URI.

    How is this country term being determined? Unless it’s something selected by the user as a setting, I’d advise you to rethink this strategy. Automatically going to a language without user input makes for a poor experience. Not everyone in the US may want English content for example. They might be visiting from another country, or English is not the US resident’s native language and they prefer their native language be used.

    And how is the country code going to be used? I assumed language in the last paragraph. I realize that may not be the intent. Maybe products are filtered by what can be exported to the US. Even then, maybe the user is having the product shipped to their child in another country with lax import restrictions. Restricting content based on some automatic criteria rarely works well. Be sure this has been well thought out before implementing it.

    You would also be inserting country codes into site URLs output as links. There are a number of filters for this, depending on post type. “post_link”, “page_link”, and “post_type_link” for any other types.

    Thread Starter Vikas Khunteta

    (@vikas_khunteta)

    Thanks for your answer, I think you didn’t understand what I want.

    Let me explain you my site scenario, when you visit that site, it filters or you can say restricts the content which is not for your country. The country code will be used only for above stated purpose not for language detection.

    I can add country code using add_rewrite_tag() function but that comes after subfolder, the subfolder is the folder where the site hosted. I want to rewrite subfolder with the country code.

    Is this possible to rewrite the subfolder with country code?

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Rewrite sub folder dynamically with country code in WordPress using PHP’ is closed to new replies.