0

I have a WordPress blog, and I'd like to redirect all requests to one certain post/page, excluding js, css (and images).

so example.com/some-post/ must redirect (temporarily) to exmaple.com/catch-all/

I was playing around with this, but doesn't quite seem to work (I'm not sure I need to use $uri):

if ($uri !~ \.css$) {
        set $redi C;
}

if ($uri !~ \.js$) {
        set $redi "($redi)J";
}

if ($uri !~ /catch-all/$) {
        set $redi "($redi)P";
}

if ($redi = CJP) {
        rewrite ^(.*)$ https://example.com/catch-all/ last;
        return 302;
}
4
  • do not use if ($uri - use location Jan 17, 2012 at 12:08
  • but you can't use location != /catch-all/$ as far as I know?
    – Tuinslak
    Jan 17, 2012 at 12:27
  • regular expressions have thing called negative look behind. BTW do you really need != /catch-all/$? Jan 17, 2012 at 13:37
  • With the IFs if I wouldn't include != /catch-all it would redirect that page as well (endless loop).
    – Tuinslak
    Jan 17, 2012 at 14:33

1 Answer 1

7

Use empty location blocks to avoid the redirect for certain requests:

location ~ \.css$ {}
location ~ \.js$ {}
location /catch-all/ {}
location / {
    rewrite ^(.*)$ /catch-all/ redirect;
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .