简体   繁体   中英

IIS7 URL Redirect with Regex

I'm preparing for a major overhaul of our shopping cart, which is going to completely change how the urls are structured. For what its worth, this is for Magento 1.7.

An example URL would be: {domain}/item/sub-domain/sub-sub-domain-5-16-7-16-/8083770?plpver=98&categid=1027&prodid=8090&origin=keyword

and redirect it to {domain}/catalogsearch/result/?q=8083710

My web.config is:

<?xml version="1.0" encoding="UTF-8"?>
   <configuration>
    <system.webServer>
    <rewrite>
    <rules>
     <rule name="Magento Required" stopProcessing="false">
      <match url=".*" ignoreCase="false" />        <conditions>
                        <add input="{URL}" pattern="^/(media|skin|js)/" ignoreCase="false" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions> <action type="Rewrite" url="index.php" />
    </rule>
                <rule name="Item Redirect" stopProcessing="true">
                    <match url="^item/([_\-a-zA-Z0-9]+)/([_\-a-zA-Z0-9]+)/([_\-a-zA-Z0-9]+)(\?.*)" />
                    <action type="Redirect" url="catalogsearch/result/?q={R:3}" appendQueryString="true" redirectType="Permanent" />
                    <conditions trackAllCaptures="true">
                    </conditions>
                </rule>
   </rules>
   </rewrite>
        <httpProtocol allowKeepAlive="false" />
        <caching enabled="false" />
        <urlCompression doDynamicCompression="true" />
  </system.webServer>
</configuration>

Right now it seems the redirect is completely ignored, even though in the IIS GUI the sample url passes the regex test. Is there a better way to redirect or is there something wrong with my web.config?

Turns out the answer is here . Can't have a Question Mark in the regex, as IIS treats it as a query string and it needs to be captured in the "conditions" section of the web.config.

So I just took (\\?.*) off the end of regex, and added:

<conditions logicalGrouping="MatchAny" trackAllCaptures="true">
    <add input="{QUERY_STRING}" pattern=".*" />
</conditions>

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