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;
}
link|improve this question

79% accept rate
do not use if ($uri - use location – SaveTheRbtz Jan 17 at 12:08
but you can't use location != /catch-all/$ as far as I know? – Tuinslak Jan 17 at 12:27
regular expressions have thing called negative look behind. BTW do you really need != /catch-all/$? – SaveTheRbtz Jan 17 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 at 14:33
feedback

1 Answer

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

location ~ \.css$ {}
location ~ \.js$ {}
location /catch-all/ {}
location / {
    rewrite ^(.*)$ /catch-all/ redirect;
}
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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