简体   繁体   中英

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>

You may use

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM