.htaccess – WordPress under Magento site with multi language sub directories

tl; dr

I have a Magento install on www.example.com

Read More

I have a WordPress install on www.example.com/wordpress

And I need the following urls to also serve that same wordpress content;

www.example.com/eu/wordpress
www.example.com/gb/wordpress

P.S.: I know there are duplicate content issues with this, please ignore that

The question is: What’s the best way to do that?

The full story

I have a Magento multi store site using the 2 digit language code subdirectory technique.

I have one WordPress installation in it’s own subdirectory.

app
downloader
errors
eu/ - symlinks for the € Euro store
gb/ - symlinks for the £ UK store
includes
js
lib
media
shell
wordpress/ - The WordPress install
var

I need the WordPress blog to be available from all stores so they user stays in the store with their locale/currency.

What I have Tried

Using the answers in these Stacks;

htaccess multi language site with sub directories, and default 301

Endless Redirect Loop by htaccess rules multi language

I’ve made attempts but unfortunately I am terrible with .htaccess and vhosts problems

Via the vhosts file

<VirtualHost *:80>
    ServerName www.example.com/eu/wordpress/
    ServerAlias www.example.com/wordpress/
    DocumentRoot /var/www/vhosts/www.example.com/public/wordpress
</VirtualHost>

<VirtualHost *:80>
    ServerName www.example.com/gb/wordpress/
    ServerAlias www.example.com/wordpress/
    DocumentRoot /var/www/vhosts/www.example.com/public/wordpress
</VirtualHost>

Via the WordPress htaccess

RewriteCond %{REQUEST_URI} !^/(eu|gb )/wordpress(/|$) [NC]
RewriteRule ^(.*)$ wordpress/$1 [R=301,L]

Via the Magento .htaccess

RewriteCond %{REQUEST_URI} ^/eu/wordpress/(.*)$ [OR]
RewriteCond %{REQUEST_URI} ^/gb/wordpress/(.*)$ [OR]
RewriteRule ^/wordpress/.*$ - [L]

Related posts

2 comments

  1. Firstly, you don’t need vhosts for wordpress, you need only one vhost per domain and/or subdomains. Your vhost should look something like this(which I assume that you already have a vhost similar to this for your magento shop):

    <VirtualHost *:80>
        ServerName example.com
        ServerAlias www.example.com
    
        DocumentRoot /var/www/vhosts/www.example.com/public
    
        <Directory "/var/www/vhosts/www.example.com/public">
            # allow .htaccess files to override all directives
            AllowOverride All
        </Directory>
    </VirtualHost>
    

    Now, you just need to modify magento’s .htaccess(/var/www/vhosts/www.example.com/public/.htaccess), and add the following rules:

    <IfModule mod_rewrite.c>
        RewriteEngine on
    
        # rewrite rule to redirect
        # eu/wordpress  -> /wordpress/
        # eu/wordpress/ -> /wordpress/
        # gb/wordpress  -> /wordpress/
        # gb/wordpress/ -> /wordpress/
        RewriteRule ^(eu|gb)/wordpress/?$ /wordpress/ [R=301,NC,L]
    
        # ... continue here with magento's rewrite rules ...
    </IfModule>
    
  2. We have this in production, hopefully this answer might help someone at some point.

    The way we’ve done this is to add a RUN_CODE environmental variable which is used in a custom WordPress filter to get the urls all working. I’ve used the ‘eu’ example from my question to illustrate it below. Note we had to do this for both Apache and Nginx so I’ve added the .htaccess and server blocks for both.

    APACHE – .htaccess

    In the eu country stub subdirectory you add this to your .htaccess file (so eu/.htaccess)

    RewriteCond %{REQUEST_URI} ^/eu/wordpress$
    RewriteRule ^(.*)$ /eu/wordpress/ [R=301]
    
    RewriteCond %{REQUEST_URI} ^/eu/wordpress(.*)$
    RewriteRule ^(.*)$ /wordpress/index.php/$1 [L,E=RUN_CODE:eu]
    

    NGINX – Server Block

    location ~* /eu/wordpress(.*) {
            if (!-f $request_filename) {
                    set $code 'eu';
                    rewrite ^(.*)$ /wordpress/index.php?$1 last;
                    break;
            }
    }
    

    In the WordPress root index.php this was added at the top of the file

    $pos = strpos($_SERVER['REQUEST_URI'], '/wordpress');
    if ($pos !== 0) {
        $_SERVER['REQUEST_URI'] = substr($_SERVER['REQUEST_URI'], $pos);
    }
    

    WordPress theme functions.php

    add_filter('post_link', 'link_mcnab');
    add_filter('page_link', 'link_mcnab');
    add_filter('bloginfo_url', 'link_mcnab');
    function link_mcnab($link)
    {
        if (isset($_SERVER['REDIRECT_RUN_CODE']) && $_SERVER['REDIRECT_RUN_CODE']) {
            $homeUrl = home_url();
            $domain = substr($homeUrl, 0, strrpos($homeUrl, '/'));
            $link = str_replace($domain, $domain . '/' . $_SERVER['REDIRECT_RUN_CODE'], $link);
        }
    
        return $link;
    }
    

Comments are closed.