简体   繁体   中英

Trouble with converting IIS rewrite rule to Apache rewrite rule

I'm having trouble converting a IIS rewrite rule to Apache.

The purpose of the rule is to redirect domain.com/c/name/ to domain.com/?company=name . It should also redirect domain.com/c/name/?page=me to domain.com/?page=me&company=name .

This is the original IIS web.config file:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
    <rewrite>
        <rules>
            <rule name="multi-company management" enabled="true">
                <match url="^c/(.+?)/(.*)" />
                <action type="Rewrite" url="{R:2}?company={R:1}" logRewrittenUrl="false" />
            </rule>
        </rules>
    </rewrite>
    <tracing>
        <traceFailedRequests>
            <add path="*">
                <traceAreas>
                    <add provider="WWW Server" areas="Rewrite" verbosity="Verbose" />
                </traceAreas>
                <failureDefinitions statusCodes="200-399" />
            </add>
        </traceFailedRequests>
    </tracing>
</system.webServer>
</configuration>

The apache url I came up with gives the error ERR_TOO_MANY_REDIRECTS and is the following:

RewriteEngine on
RewriteRule ^c/(.+?)/(.*)$ /$2?company=$1

I think i'm almost there but lacking something to avoid keep redirecting. I think the rule keeps matching on every request. Any help would be greatly appreciated.

I found a working solution.

RewriteEngine on

#Rewrite JS, CSS and Images
RewriteCond %{REQUEST_URI} (.css|.js|.css.php|.png|.jpg|.jpeg|.gif)$
RewriteRule ^c/(.+?)/(.*) /$2 [L]

#Rewrite php pages (adding company as request param)
RewriteCond %{REQUEST_URI} (.php|/)$
RewriteRule ^c/(.+?)/(.*)$ /$2?company=$1 [QSA]

There are probably better ways but this seems to work so far.

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