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've setup my first Nginx site and the important rewrites are all done. They had to be manual, no rules would possibly have handled it, so I have a raft of rules like this to handle old URLs that might be indexed or bookmarked:

rewrite ^/html/Air_III.html /designers-elements-air permanent;

There is another set of odd files that seem to get traffic, in parts of the site that we haven't gotten to yet so I just copied them over. Most of these were renamed .php from the old .html and are otherwise unchanged, but there are probably some that are still .html.

I'm looking for a rule that will handle the case of a request for any .html file, in any directory, that doesn't actually match a current file by that name and will rewrite to a .php file if it exists.

In "Apache speak" this would be a redirect permanent rather than a rewrite. I'd rather not track all these down and do them by hand!

share|improve this question

1 Answer 1

up vote 0 down vote accepted

You'll need an if statement to check the file existance and rewrite appropriately:

location ~ \.html$ {
    if (!-f $request_filename) {
        rewrite ^(.*)\.html$ $1.php permanent;
    }
}
share|improve this answer
    
Sorry for the delay, this worked flawlessly without any changes. –  G. Armour Van Horn Mar 12 '13 at 21:50

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.