Configure URL rewriting rule for a specific domain

To test the local dev, I need to catch all requests from www.somedomain.com/XXX (where X is the path) and send them to localhost/somevdir/XXX .

I added this to my HOSTS file (c: \ windows \ system32 \ drivers \ etc):

 127.0.0.1 www.mydomain.com 

Then in IIS8 (Windows 8) I added a binding to my "Default Web Site" for the site www.mydomain.com. This works, now I can view www.mydomain.com/test.html and see the html test page. My virtual directory is inside the default website. Then I add the URL rewrite URL to the website for the last bit:

 <rewrite> <rules> <rule name="mydomain.com" stopProcessing="true"> <match url="^www.mydomain.com/(.*)" /> <action type="Rewrite" url="localhost/MyVDir/{R:1}" logRewrittenUrl="true" /> </rule> </rules> </rewrite> 

But - it does not work. I get 404, so it looks like a match never happens. I tried redirecting and rewriting, and I tried without ^ in the regex and several other regex tags. Can someone explain what I did wrong?

+8
source share
2 answers

I think the following should work:

 <rule name="mydomain.com" stopProcessing="true"> <match url="(.*)" /> <conditions logicalGrouping="MatchAll"> <add input="{HTTP_HOST}" pattern="^(mydomain\.com|www\.mydomain\.com)$" /> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> </conditions> <action type="Redirect" url="http://localhost/MyVDir/{R:1}" redirectType="Temporary" /> </rule> 

Matching at any URL ensures that the conditions are checked, and the HTTP_HOST server variable appears to be the most reliable way to validate the requested host name. You can remove the REQUEST_FILENAME input condition, but it works like a pretty nice health check to make sure static files are always served.

+16
source

The following is best used to intercept www. domain versions www. and non- www. so that you don’t have to write the domain twice, which can lead to errors, as the typo is twice as likely. (A bracket with a ? Means optional in terms of regular expressions.)

 <rule name="mydomain.com" stopProcessing="true"> <match url="(.*)" /> <conditions logicalGrouping="MatchAll"> <add input="{HTTP_HOST}" pattern="^(www.)?mydomain\.com$" /> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> </conditions> <action type="Redirect" url="http://localhost/MyVDir/{R:1}" redirectType="Temporary" /> </rule> 
+1
source

Source: https://habr.com/ru/post/947100/


All Articles