1

I'm running a multilingual wiki (MediaWiki 1.26.2 with MobileFrontend) on nginx 1.9.3/OpenBSD 5.8.

For each language wiki, I have a separate MediaWiki installation folder and a subdomain like en.domain.com pointing to that folder.

I'd like to add a subdomain like en.m.domain.com for the mobile view using the MediaWiki installation folder of the desktop view but with an appended &mobileaction=toggle_view_mobile (or ?mobileaction=toggle_view_mobile with a question mark instead of an ampersand if there is already an argument).

I'm also using CORS, short URLs and redirects from http:// to https://.

This is how my server block looks like:

server {
    listen 80;
    server_name en.m.domain.com;
    root /path/to/domain/en;
    index index.html index.htm index.php;
    autoindex off;

# CORS
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Credentials' 'true';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'X-Requested-With, Accept, Content-Type, Origin';

# Redirect to https://

    if ($http_cf_visitor ~ '{"scheme":"http"}') {
        return 301 https://$server_name$request_uri;
        }

    location = / {
        return 301 https://en.m.domain.com/wiki/Main_Page;
        }

    location = /w {
        return 301 https://en.m.domain.com/wiki/Main_Page;
        }

    location = /w/ {
        return 301 https://en.m.domain.com/wiki/Main_Page;
        }

    location = /wiki {
        return 301 https://en.m.domain.com/wiki/Main_Page;
        }

    location = /wiki/ {
        return 301 https://en.m.domain.com/wiki/Main_Page;
        }

# Short URLs

    location / {
        index index.php;
        error_page 404 = @mediawiki;
        }

    location @mediawiki {
        rewrite ^/wiki([^?]*)(?:\?(.*))? /w/index.php?title=$1&$2 last;
        }

    location ~ \.php5?$ {
        try_files $uri =404;
        include fastcgi_params;
        fastcgi_pass   127.0.0.1:1234;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors on;
        }

    location ~ \.php?$ {
        try_files $uri =404;
        include fastcgi_params;
        fastcgi_pass   127.0.0.1:1234;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors on;
        }

# Append mobileaction=toggle_view_mobile for mobile version

    location / {                                                   

        # If there are no arguments add a question mark
        if ($args = '') {                                            
        set $new_request_uri "$request_uri?mobileaction=toggle_view_mobile";  
        }                                                            

        # If there are already arguments add an ampersand
        if ($args != "") {      
        set $new_request_uri "$request_uri&mobileaction=toggle_view_mobile";  
        }

        rewrite $new_request_uri last;        
        }       

}

Unfortunately the mobileaction=toggle_view_mobile part doesn't work :(

Any ideas how to fix this?

Thanks and cheers,

Till

0

There are multiple problems with your current implementation: you have two location / blocks and rewrite $new_request_uri last; is semantically incorrect.

The easy solution is to modify the $request_uri by performing an external redirect. This is messy because you need to identify only those URIs which do not have a mobileaction argument. For example:

if ($args !~* mobileaction) {
    rewrite ^ $uri?mobileaction=toggle_view_mobile permanent;
}

The rewrite directive takes care of ? vs & and automatically appends the existing argument list.

The if block could be placed inside the location / block or above it.

  • Thank you so much for your fast help! And if I want nginx to look exactly for mobileaction=toggle_view_mobile, I need to change the first line to if ($arg_mobileaction !~* toggle_view_mobile) {, right? And is it possible to make mobileaction=toggle_view_mobile invisible for short URLs? I tried rewrite ^/wiki([^?]*)(?:\?(.*))? /w/index.php?title=$1&$2&mobileaction=toggle_view_mobile last; and rewrite ^/wiki([^?]*)(?:\?(.*))? /w/index.php?title=$1&$args last; but that didn't work :( – Till Kraemer Feb 1 '16 at 19:53
  • Yes. And you're right - it doesn't work, which is why I suggested the external redirect method. An internal method would involve looking inside the fastcgi_params file and replacing REQUEST_URI with something new. – Richard Smith Feb 1 '16 at 20:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.