Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

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;
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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