Search code examples
apache.htaccessmod-rewrite

htaccess rewrite to reverse subfolders


As basic as it gets, here is an example of what I'm trying to do: enter image description here

I have a structure that takes different types of CMS' and inside each of those installations, are language variations.

By default the structure looks like this: https://[domain]/[country]/[installation]/[language-code]

Ultimately, I'm trying to get this:

https://[domain]/[country]/[language-code]/[installation]/

I'm wondering if this can be accomplished, due to the fact the folders/languages themselves are not actual folders and in fact rewrites from the CMS' settings (within the subfolders).

As unmodified these would be examples of the current urls:

https://example.com/ca/events/en/
https://example.com/ca/store/en/
https://example.com/ca/network/en/

And example of the desired results:

https://example.com/ca/en/events/
https://example.com/ca/en/store/
https://example.com/ca/en/network/

and for each installation, they dont always have the same languages (eg. ca has en and fr, but us has en and es)

Note: subsequent pages would be appended... eg: https://example.com/ca/en/events/event-name/subpage/


Solution

  • You may be be able to use this single redirect rule on top of your .htaccess:

    RewriteEngine On
    
    RewriteRule ^([a-z]{2})/(.+)/([a-z]{2}/?)$ $1/$3/$2 [L]