0
votes

I am having a scenario that if URL doesn't contain anything in URI segment then no need to rewrite to index.php but if contains then rewrite it.

For Example: http://www.example.com/ or http://www.example.com No Rewrite Rule Required

But if URL is http://www.example.com/homepage or http://www.example.com/user/1 or http://www.example.com/edit-user/123/456 then it should be rewrite to index.php

I tried below.

<rules>
    <rule name="custom rule 1" stopProcessing="true">
    <match url="^([^/]+)/?$"  />
    <action type="Rewrite" url="index.php"  />
</rule>

It is working for http://www.example.com/ and http://www.example.com/homepage but showing 404 for others pages.

The URI segments are dynamic and it can be multilingual.

Technologies used IIS, PHP (Laravel)

1

1 Answers

2
votes

You can try this, it will match all kinds of URIs whether dynamic or multilingual.

 <rule name="custom rule">
                    <match url=".*" />
                    <action type="Rewrite" url="index.php" />
                    <conditions>
                        <add input="{REQUEST_URI}" pattern=".*" />
                   </conditions>
                </rule>

Test result

Fail request tracing

If this rule is only used for some URIs like user or edit-user, maybe this rule is more suitable.

<rule name="custom rule">
                <match url=".*" />
                <action type="Rewrite" url="index.php" />
                <conditions logicalGrouping="MatchAny">
                    <add input="{REQUEST_URI}" pattern="user/(.*)" />
                    <add input="{REQUEST_URI}" pattern="edit-user/(.*)" />
                </conditions>
            </rule>