简体   繁体   中英

www Rewrite Rules for IIS Web.config Not Working

I have a rewrite rule in IIS that is supposed to rewrite inbound URL's without the 'www' to 'www'. The rewrite rule is in fact rewriting the URL to include the 'www', but it's directing the visitor to /index.php instead of sending them to the link they were trying to reach.

In case you're wondering about the php extension, I am using Joomla on Windows. My rewrite rule is written in the web.config file. Currently my non-www to www rule is the 3rd rule, which I am guessing may be the problem. The first two rewrite rules are the Joomla default rules for SEF. Does anyone know if moving it to the first rule will resolve the problem?

 <rewrite> <rules> <rule name="Joomla! Rule 1" stopProcessing="true"> <match url="^(.*)$" ignoreCase="false" /> <conditions logicalGrouping="MatchAny"> <add input="{QUERY_STRING}" pattern="base64_encode[^(]*\\([^)]*\\)" ignoreCase="false" /> <add input="{QUERY_STRING}" pattern="(>|%3C)([^s]*s)+cript.*(&lt;|%3E)" /> <add input="{QUERY_STRING}" pattern="GLOBALS(=|\\[|\\%[0-9A-Z]{0,2})" ignoreCase="false" /> <add input="{QUERY_STRING}" pattern="_REQUEST(=|\\[|\\%[0-9A-Z]{0,2})" ignoreCase="false" /> </conditions> <action type="CustomResponse" url="index.php" statusCode="403" statusReason="Forbidden" statusDescription="Forbidden" /> </rule> <rule name="Joomla! Rule 2" enabled="true"> <match url="(.*)" ignoreCase="false" /> <conditions logicalGrouping="MatchAll"> <add input="{URL}" pattern="^/index.php" ignoreCase="true" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" /> </conditions> <action type="Rewrite" url="index.php" /> </rule> <rule name="Redirect to WWW" stopProcessing="true"> <match url=".*" /> <conditions> <add input="{HTTP_HOST}" pattern="^example.com$" /> </conditions> <action type="Redirect" url="http://www.example.com/{R:0}" redirectType="Permanent" /> </rule> </rules> </rewrite> 

Your first rule (Joomla 1) is a security rule. It can stay unchanged (looks OK).

Your second rule (Joomla 2) rewrites every non-existing file/folder to /index.php .
Remove following line from this rule

<add input="{URL}" pattern="^/index.php" ignoreCase="true" negate="true" />

Finally, your last rule ( www rule) should look as follow

<rule name="Redirect to WWW" stopProcessing="true">
   <match url="^(.*)$" />
   <conditions>
      <add input="{HTTP_HOST}" pattern="^www\." negate="true" />
   </conditions>
   <action type="Redirect" url="http://www.{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>

Note: would be better to put your www rule in first place (or, at least, in second place)

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