Andrew
Andrew

Reputation: 795

Regex to match file name with or without extension

How do I modify ^(.*/)index\.html$ properly to match index.html with or without extension ?

example.com/index.html - match        
example.com/index - match
example.com/indexx - ignore

if ($request_uri ~* "^(.*/)index\.html$") {
    return 301 $1;
}

Upvotes: 0

Views: 227

Answers (1)

ctwheels
ctwheels

Reputation: 22817

Wrapping \.html into a capture group and making it optional ? (match zero or one time) allows you to make the extension optional:

# your regex:
^(.*/)index\.html$

# change to:
^(.*/)index(\.html)?$
#          ^      ^^

Upvotes: 2

Related Questions