Convert web.config file to .htaccess

2019-02-16 01:39发布

问题:

I'm looking for a way to convert my IIS rewrite rules to an .htaccess file. I couldn't find any tool to automatically do this and all I can get myself is 500 Internal Server Errors.

The web.config file looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <clear />
                <rule name="RemoveTrailingSlash" stopProcessing="true">
                    <match url="^(.*?)/+$" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    </conditions>
                    <action type="Redirect" url="http://{HTTP_HOST}/{R:1}" />
                </rule>
                <rule name="HaltOnTinyMCE" stopProcessing="true">
                    <match url="^tinymce/.*$" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
                    <action type="None" />
                </rule>
                <rule name="RewriteContent" enabled="true" stopProcessing="true">
                    <match url="^.*\.(gif|jpg|png|css|js|swf|txt|xml)$" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
                    <action type="Rewrite" url="domains/{HTTP_HOST}/{R:0}" appendQueryString="true" logRewrittenUrl="false" />
                </rule>
                <rule name="RewritePages" enabled="true">
                    <match url="^.*$" ignoreCase="true" negate="false" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
                    <action type="Rewrite" url="index.php" logRewrittenUrl="false" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

My best try at manually converting this is still giving me the 500 Internal Server Error. Any suggestions?

RewriteEngine on

RewriteCond /+$
RewriteRule ^(.*?)/+$ http://{HTTP_HOST}/$1 [R=301,L]

RewriteCond ^tinymce/.*$
RewriteRule ^(.*)$ $1 [L]

RewriteCond \.(gif|jpg|png|css|js|swf|txt|xml)$
RewriteRule ^.*\.(gif|jpg|png|css|js|swf|txt|xml)$ domains/{HTTP_HOST}/$1 [L]

RewriteRule ^.*$ index.php

Thanks in advance!

回答1:

You were heading in the right direction, but there were a few errors, and unneeded things. This is what I came up with:

RewriteEngine on
RewriteBase /

#remove tailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ http://%{HTTP_HOST}/$1 [R=301,L]

# stop rules below from being applied if url starts with tinymce/
RewriteRule ^tinymce/.*$ - [L]

#rewrite images etc. to domains folder (unless url already starts with domain/)
RewriteCond $0 !^domains/
RewriteRule ^.*\.(gif|jpg|png|css|js|swf|txt|xml)$ domains/%{HTTP_HOST}/$0 [L]

#catch all other (I added 2 rewriteconds, to prevent rewriting existing files (including index.php))
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php