1

I am trying to make a redirect for https://mypage.com/foo to https://mypage.com/bar while still keeping https://mypage.com/foo in the browser url. I have tried several combinations of different rewrite flags, putting them into the location block etc., but nothing worked so far.

The target CMS (Contao) uses app.php as an entry and pages can be requested like /app.php/foo?param=value. The rewrite rule when not using the permanent flag is not applied at all and the REQUEST_URI in app.php always outputs foo.

my config looks like this:

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    root /var/www/mypage/web;

    server_name mypage.com;

    include /mydir/redirect-old-links.conf; # contains permanent rewrite rules with hardcoded links

    rewrite ^(.*).html(\?.*)?$ $1 permanent; # redirect to remove .html suffix
    rewrite ^/foo /bar; # <--- This won't have an effect

    # ... ssl stuff

    location / {
        index app.php index.html;
        try_files $uri $uri/ /app.php?$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }

    location = /robots.txt {
        add_header Content-Type text/plain;
        return 200 "User-agent: *\nDisallow: /\n";
    }
}

Can someone please help me with this?

  • A redirect changes the URL in the browser bar. What you describe is a "rewrite". You use the term "rewrite" a couple times correctly, but you should never refer to it as a redirect. – Stephen Ostermiller Oct 1 '18 at 9:57
  • I'm not very familiar with nginx syntax. Under Apache, leading slashes are often the problem depending on which file the rewrite is in. Have you tried rewrite ^foo bar? – Stephen Ostermiller Oct 1 '18 at 9:59
  • @StephenOstermiller No that didn't work, either. – olastor Oct 7 '18 at 7:35

Your Answer

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

Browse other questions tagged or ask your own question.