Forum Moderators: open

Message Too Old, No Replies

Redirecting on IIS

does web.config work like htaccess

         

chewy

1:03 am on Feb 1, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi,

Working on a transition from asp to coldfusion.

I know, I know, sort of going in the wrong direction but the situation was cast in stone long before I came into the situation.

so I'm used to htaccess for redirection.


Simple redirects work like this:

Redirect permanent /widget.asp http://www.example.net/widget.cfm
Redirect permanent /bigger-widget.asp http://www.example.net/bigger-widget.cfm


works great (and I can use regex etc)


But in IIS, does it work the same way? Will this work in web.config?

<configuration>
<location path="widget.asp">
<system.webServer>
<httpRedirect enabled="true" destination="http://example.com/widget.cfm" httpResponseStatus="Permanent" />
</system.webServer>
</location>
<location path="bigger-widget.asp">
<system.webServer>
<httpRedirect enabled="true" destination="http://example.com/bigger-widget.cfm" httpResponseStatus="Permanent" />
</system.webServer>
</location>
</configuration>

Is it as simple as that?

-C

[edited by: phranque at 2:05 am (utc) on Feb 1, 2015]
[edit reason] exemplified domains [/edit]

blend27

2:50 pm on Feb 12, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi Chewi,

I think you should use only one system.webServer directive.

What I usually do in web.config is


<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<!-- rewrite maps -->
<rewriteMaps configSource="webrwmaps.config"></rewriteMaps>

<!-- rewrite rules and redirects-->
<rules configSource="webrwrules.config"></rules>
</rewrite>

<httpErrors errorMode="Custom">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" path="404-notfound.cfm" responseMode="File" />
</httpErrors>


</system.webServer>
</configuration>


so
------------------------------------
<!-- rewrite maps webrwmaps.config, this is where we rewrite to SEF URLS -->
<rewriteMaps>
<rewriteMap name="category" defaultValue="" ignoreCase="false">
<add key="/blue-widgets.html" value="/category-listing.cfm?category=100" />
<add key="red-widgets.html" value="/category-listing.cfm?category=100" />
<!-- etc, etc... -->
</rewriteMap>
<!-- StaticRedirects -->
<rewriteMap name="StaticRedirects">
<add key="/old-blue-widgets.aspx?id=100" value="/blue-widgets.html" />
<add key="/old-red-widgets.aspx?id=200" value="/red-widgets.html" />
</rewriteMap>

</rewriteMaps>

--------------
Then we have the rules for internal rewrites and URL redirects in webrwrules.config

<rules>
<!-- this one is for rewrites -->
<rule name="Rewrite rules for category">
<match url=".*" />
<conditions>
<add input="{category:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="{C:1}" appendQueryString="false" />
</rule>
<!-- this one is for redirects -->
<rule name="Redirect Rule" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{StaticRedirects:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="https://servername.tld{C:1}" appendQueryString="False" redirectType="Permanent" />
</rule>
</rules>


To see the actual REQUEST_URI:

in application.cfc

<cffunction name="OnRequestEnd" access="public" returntype="void" output="true" hint="Fires after the page processing is complete.">
<cfoutput>REQUEST_URI: #CGI.HTTP_X_ORIGINAL_URL#</cfoutput>
<cfreturn />
</cffunction>

------------------------------------------------------

This way you could dynamically/programmatically generate you maps using the programming language of your choice without actually using URL Rewrite 2.0 GUI.

For this to work, you will need to be on IIS7+ with URL Rewrite module installed, there is a lot of info here: [iis.net...] and here
[iis.net...]


I have same setup in IIS 7.5/Windows Server 2008 R2 and ColdFusion 10, works like a charm, and feel free to regexp away!


-------------------------------------------

Most CF hosting providers also offer ISAPI rewrite installed, you could also use that, as in .HTACCESS.

I tend to only use .HTACCESS when needed on case by case bases.

Blend27

chewy

12:49 am on Feb 13, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



thanks - current host will not allow ISAPI rewrite.

fortunately, we found another way to do it though.

chewy

1:25 am on May 5, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



now we're having some new and odd canonical issues !

w.site.com is the same as ww.site.com is the same as www.site.com. AK!

never seen that one before - here's the web.config file that is supposed to be doing the trick.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to WWW" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^site.com$" />
</conditions>
<action type="Redirect" url="http://www.site.com/{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

phranque

2:05 am on May 5, 2015 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



your rule essentially says - [if the requested hostname is the root domain (example.com), redirect the request to http://www.example.com/] and what you want is something that says - [if the requested hostname is not precisely www.example.com, redirect the request to http://www.example.com/]

you should make that corresponding change to the condition.

chewy

9:48 am on May 5, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes phranque, totally agreed, you are right on it - just what is the perfect regex to do it? I know enough to know what I do not know and was hoping to find the cookbook for such here, or at least get pointed to it.

I'll dig around some more.

Glenn's been able to help out on these matters over on the Linux side (and I still use that code to this day on numerous sites).

Is there anyone here who can add the correct regex to solve the single-dub, double-dub problem?

phranque

10:08 am on May 5, 2015 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



maybe something like this:
<add input="{HTTP_HOST}" pattern="^www\.example\.com$" negate="true" />

chewy

4:18 pm on May 5, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Most absolutely awesome. Thanks! Another one solved by WebmasterWorld magicians!