.htaccess apache URL rewriting

I have performed multiple searches and have yet to find what i am looking for.

I need to perform some URL rewrites but I don’t have the expertise and knowledge to code the rewrites. I have just begun learning regex and apache URL rewriting.

Read More

Onto the problem.
The website I would like to use these re-directs on is a .com domain so users will be coming from multiple countries. On the website there is an online shop. On the initial landing page you are able to select which country you are from and it will take you to

www.domain.com/*country*/

which has the online shop.

Now this is where it gets complicated.
Depending on the country the user clicked, I would like to append that countries name to the URL after they have clicked on a product and entered the shop.

E.G "www.domain.com/*country*/*product-category*/

At the moment, when you click on a product after selecting your country it goes to

www.domain.com/*product-category*/

The site is running WordPress.
Not sure what other information is required.

Thanks Guys!

Related posts

Leave a Reply

2 comments

  1. If you have control over the implementation of that online shop implementation then a solution might be quite easy: as said you somehow have to make sure that all urls sent out from that shop, so urls one can click/select inside the shop navigation, carry the chosen country coding within. Since the shop is called with that information in the first place the information is available and actually perfectly easy usable the way you describe the structure of the base url: www.domain.com/*country*/. Easy to use since the country code is part of the base url of the shop, not some arbitrary argument that has to be handled in an explicit manner. So all you have to take care of is this: make the shop use relative links in all urls it sends out to the client. Relative links are just relative paths (so a path without a leading slash(/)). Browsers handle relative links by prepending the base url of the current page loaded to create a full url. So in your situation this means:

    • base url: www.domain.com/*country*/
    • relative link: product/123456/action/buy (just an example)
    • url composed by the browser: www.domain.com/*country*/product/123456/action/buy

    This is exactly what you want: you do not have to handle/specify the country code in an explicit manner, yet it is contained in all urls. You “just” have to make sure you consequently use relative links in that shop implementation.

    A side note: in general one should always prefer relative links to absolute links or urls, since it keeps implementations flexible, relocatable and portable. Only references to external targets should be coded as urls.

    Other approaches might be these:

    • using that “landing page” as request relay for the online shop by routing all requests. This means you enter a scripting level in the routing/rewriting process, so you have more means at hand. In specific you could use php sessions at routing level.
    • changing the structure of that base url: using virtual hosts instead of the urls path to code the chosen country, so domain names like mozambique.domain.com or spitzbergen.domain.com. Most likely the domain name is automatically taken into account by the existing implementation, although all virtual hosts share the same single installation.
    • considering the “referrer url” of each incoming request during the rewriting process. This actually would be very easy to implement, but I am afraid it is not really safe for all browsers, since that relies on the http referrer header to be present in all requests, which can be blocked easily. Also in that case deep urls would lead to an error, since required information is simply missing.
    • changing the target of urls on-the-fly by means of javascript. This has the advantage that you do not have to change anything in the shops implementation, except of referencing a single script file in the header. That script contains code which changes the target url of all scripts or form submissions initiated by the user. But again, like in the previous approach, this does not appear to be robust, since it requires the use of javascript which may be disabled on the client side. On the other hand this does not mean it is insecure (being secure and being robust are two different things), since a missing country code does not hold a risk, it just leads to an error that has to be handled.