Beems
Beems

Reputation: 810

IIS URL Rewrite to Redirect PDF File (Regex)

Server: IIS 8

I have a URL Rewrite rile to redirect PDF files to a page that will handle some additional processing. Everything works good except when the PDF files have special characters or spaces in them, then the destination page only gets the characters right of any spaces or special characters.

As an example, see the following filename:

Receipt - Hard Drive.PDF

the receiving page (/getfile/?PDF=) would receive only

Drive.PDF

I have tried various regex methods but as with most people my regex knowledge is pretty terrible.

How can I write a 'match URL' that will accept all filenames (at least those accepted by Windows, such as filenames with underscores, dashes, spaces, single quotes, double quotes, pound signs, etc). Is there any way to write something universal that simply passes all characters regardless of what they are, since I really only want to match *.pdf? My current rule is below.

<rule name="PDF Rewrite" stopProcessing="true">
    <match url="([\w-]+)\.pdf$" />
    <action type="Redirect" url="/getfile/?PDF={R:1}.pdf" logRewrittenUrl="true" redirectType="Temporary" />
</rule>

Upvotes: 1

Views: 2494

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626870

You may use

<match url="(.+)\.pdf$" />

The .+ matches one or more characters other than line break chars as many as possible.

Upvotes: 4

Related Questions