Search code examples
.htaccessmod-rewriteiisweb-configweb.config-transform

Converting web.config file to .htaccess


Can anybody help me to convert this web.config file to .htaccess

I can find several online converters for .htaccess to web.config but none for web.config to .htaccess

<rules>
    <rule name="RedirectUserFriendlyURL1" stopProcessing="true">
        <match url="^themes/sets\.cfm$" />
        <conditions>
            <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
            <add input="{QUERY_STRING}" pattern="^set=([^=&amp;]+)$" />
        </conditions>
        <action type="Redirect" url="themes/sets/{C:1}" appendQueryString="false" />
    </rule>
    <rule name="RewriteUserFriendlyURL1" stopProcessing="true">
        <match url="^themes/sets/([^/]+)/?$" />
        <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        </conditions>
        <action type="Rewrite" url="themes/sets.cfm?theme={R:1}" />
    </rule>
    <rule name="RedirectUserFriendlyURL2" stopProcessing="true">
        <match url="^set\.cfm$" />
        <conditions>
            <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
            <add input="{QUERY_STRING}" pattern="^set=([^=&amp;]+)&amp;name=([^=&amp;]+)$" />
        </conditions>
        <action type="Redirect" url="set/{C:1}/{C:2}" appendQueryString="false" />
    </rule>
    <rule name="RewriteUserFriendlyURL2" stopProcessing="true">
        <match url="^set/([^/]+)/([^/]+)/?$" />
        <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        </conditions>
        <action type="Rewrite" url="set.cfm?set={R:1}&amp;name={R:2}" />
    </rule>
</rules>

Thanks in advance.


Solution

  • I'm a bit surprised your "friendly url 2" block didn't produce an infinite redirect loop (maybe IIS takes care of this itself, i don't know).

    Anyway, with Apache and mod_rewrite, you would have an infinite loop by just "translating" your rules.

    Note: "friendly url 1" can be translated as is, since redirect and rewrite are not exactly for the same target).

    Here is how your htaccess should look like

    RewriteEngine On
    Options -MultiViews
    
    # RedirectUserFriendlyURL1
    RewriteCond %{REQUEST_METHOD} !POST
    RewriteCond %{QUERY_STRING} ^set=([^&=]+)$
    RewriteRule ^themes/sets\.cfm$ /themes/sets/%1? [R=301,L]
    
    # RewriteUserFriendlyURL1
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^themes/sets/([^/]+)/?$ /themes/sets.cfm?theme=$1 [L]
    
    # RedirectUserFriendlyURL2
    RewriteCond %{REQUEST_METHOD} !POST
    RewriteCond %{THE_REQUEST} \s/set\.cfm\?set=([^&\s]+)&name=([^&\s]+)\s [NC]
    RewriteRule ^ /set/%1/%2? [R=301,L]
    
    # RewriteUserFriendlyURL2
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^set/([^/]+)/([^/]+)/?$ /set.cfm?set=$1&name=$2 [L]