开发者

how to use matches in conditions tag in Web.config file on IIS7?

开发者 https://www.devze.com 2023-03-28 13:36 出处:网络
I wanna convert this apache .htaccess lines to web.cofig: RewriteCond %{HTTP_HOST} ^www.(.+)$ [NC] RewriteRule ^(.*)$ http://%1/$1 [L,R=302]

I wanna convert this apache .htaccess lines to web.cofig:

RewriteCond %{HTTP_HOST} ^www.(.+)$ [NC]

RewriteRule ^(.*)$ http://%1/$1 [L,R=302]

with these codes I ca开发者_StackOverflow社区n redirect all request with www. even if my website has too many domains.


I research and found my answer. for use variables in conditions we can use {C:1}. that 1 is for first match. and my ecxact answer for this converting is:

        <rules>
            <rule name="http Redirect" stopProcessing="true">
                <match url="^(.*)$" ignoreCase="false" />
                <conditions>
                    <add input="{HTTP_HOST}" pattern="^www\.(.*)$" />
                    <add input="{SERVER_PORT_SECURE}" pattern="^0$" />
                </conditions>
                <action type="Redirect" url="http://{C:1}/{R:1}" appendQueryString="true" redirectType="Permanent" />
            </rule>
            <rule name="https Redirect" stopProcessing="true">
                <match url="^(.*)$" ignoreCase="false" />
                <conditions>
                    <add input="{HTTP_HOST}" pattern="^www\.(.*)$" />
                    <add input="{SERVER_PORT_SECURE}" pattern="^0$" negate="true" />
                </conditions>
                <action type="Redirect" url="https://{C:1}/{R:1}" appendQueryString="true" redirectType="Permanent" />
            </rule>
        </rules>

That is equal to:

RewriteCond %{HTTP_HOST} ^www.(.+)$ [NC]
RewriteCond %{HTTPS} ^off$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [L,QSA,R=302]
RewriteCond %{HTTP_HOST} ^www.(.+)$ [NC]
RewriteCond %{HTTPS} ^on$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [L,QSA,R=302]

Good Luck

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号