简体   繁体   中英

IIS Rewrite Rule to get last part of dash delimited filename

I am using IIS 8.5 and the rewrite module and am trying to come up with a rule to achieve the following

The actual URL http://www.mywebsite.com/detail/name-of-my-product-3500.php

The rewrite I want to create a rule for the above example would achieve the following http://www.mywebsite.com/detail/?id=3500

I would also be happy with anyone who has .htaccess experience to share an equivalent example as I can convert that to IIS.

So in essence my web.config rule needs to accomplish two things 1. Only apply in the /detail directory 2. Needs to pull the last part of the file name (will always be numeric)

The followign is what I come up with but this causes problems on every other url on teh site except for this within the /detail directory.

<rule name="Redirect to detail" stopProcessing="true">
                <match url="([^-]+)\/?$" ignoreCase="false" />
                <action type="Rewrite" url="/detail/?npid={R:1}" appendQueryString="false" logRewrittenUrl="true" />
            </rule>

Your rule should be like that:

<rule name="Redirect to detail" stopProcessing="true">
    <match url="^detail/.*-(\d+).php$" ignoreCase="false" />
    <action type="Rewrite" url="/detail/?npid={R:1}" appendQueryString="false"/>
</rule>

Regexp ^detail/.*-(\\d+).php$ will match with all URLs like that http://www.mywebsite.com/detail/name-of-my-product-XXXXXXXX.php and rewrite them into /detail/?npid=XXXXXXXX

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