Rewriting server variable in IIS 7.5 - url

I have a rewrite rule, which changes a server variable with the value of a subdomain.
This works on subdomain.mydomain.nl/somethinghere but not on subdomain.mydomain.nl
<rule name="Change code" enabled="true" patternSyntax="ECMAScript" stopProcessing="false">
<match url=".*" ignoreCase="true" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{SERVER_NAME}" pattern="(www\.)?(\w+)\.mydomain\.nl" />
<add input="{SERVER_NAME}" pattern="^www.mydomain.nl.*" negate="true" />
<add input="{SERVER_NAME}" pattern="^mydomain.nl.*" negate="true" />
</conditions>
<serverVariables>
<set name="MYVARIABLE" value="{C:2}" />
</serverVariables>
<action type="None" />
</rule>
I have tested 2 urls:
1: subdomain.mydomain.nl/somethinghere
2: subdomain.mydomain.nl
I retrieve the variable in PHP with the following code:
echo $_SERVER['MYVARIABLE'];
In case of URL 1, the output of this is "subdomain".
In case of URL 2, the output of this is "".
The output of URL 1 is correct, but the output of URL 2 should be "subdomain" too.
I have run a trace of both requests, and they both show that the rule is being matched and executed.
Can anyone help me?

When you set a custom server variable, you should start it with HTTP_. When you add your own header, it should start with HTTP_X_ to add a host header starting with an X.
To be honest, I can't really explain why it works without HTTP_ in some scenarios, but with HTTP_ it works in all scenarios and that's also how it's documented.
<rules>
<rule name="Change code" enabled="true" patternSyntax="ECMAScript" stopProcessing="false">
<match url=".*" ignoreCase="true" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{SERVER_NAME}" pattern="(www\.)?(\w+)\.testsite\.nl" />
<add input="{SERVER_NAME}" pattern="^www\.testsite\.nl$" negate="true" />
<add input="{SERVER_NAME}" pattern="^testsite\.nl$" negate="true" />
</conditions>
<serverVariables>
<set name="HTTP_X_MYVARIABLE" value="{C:2}" />
</serverVariables>
<action type="None" />
</rule>
</rules>
You can now get the subdomain name with echo $_SERVER["HTTP_X_MYVARIABLE"];.
I've also cleaned up your conditional regular expressions to escape the .s and also added a $ to make it truly match the exact domain names.

Related

Rerouting and rewriting URL with ARR Application request Routing IIS

The setup is the following :
i have 2 websites : one public and one private for our internal ERP. By using the same domaine name :
lesterrassesdadrien.fr.
When using the ARR it allow me to have the following : when someone ask for "lesterrassesdadrien.fr" the router will redirect the demand to the public website which is a wordpress, if the demand ask for "lesterrassesdadrien.fr/erp" it will redirect towards our erp website which is in PHP. here is the code inside the file applicationHost.config for the server in IIS:
<rewrite>
<globalRules>
<clear />
<rule name="lesterrassesdadrien_erp" patternSyntax="Wildcard" stopProcessing="true">
<match url="erp/*" />
<conditions>
<add input="{HTTP_HOST}" pattern="lesterrassesdadrien.fr" />
</conditions>
<action type="Rewrite" url="http://lesterrassesdadrien_erp/{R:1}" />
</rule>
<rule name="lesterrassesdadrien_public" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="lesterrassesdadrien.fr" />
<add input="{SERVER_PORT}" pattern="5000" negate="true" />
<add input="{SERVER_PORT}" pattern="5001" negate="true" />
</conditions>
<action type="Rewrite" url="http://lesterrassesdadrien_public/{R:1}" />
</rule>
</globalRules>
</rewrite>
</system.webServer>
<webFarms>
<webFarm name="lesterrassesdadrien_erp" enabled="true">
<server address="127.0.0.1" enabled="true">
<applicationRequestRouting httpPort="5000" />
</server>
</webFarm>
<webFarm name="lesterrassesdadrien_public" enabled="true">
<server address="127.0.0.1" enabled="true">
<applicationRequestRouting httpPort="5001" />
</server>
</webFarm>
</webFarms>
My problem is that when we click on a link inside the ERP website for exemple :
lesterrassesdadrien.fr/admin/... it will fail and bring me back to the public wordpress website : how to keep or add the "erp/" in front of all link inside my erp website?
My explanation are a bit confusing i believe.
Thanks in advance.
I tried to add this to the rules but with no aval :
<rule name="lesterrassesdadrien_admin" patternSyntax="Wildcard" stopProcessing="true">
<match url="admin/*" />
<conditions>
<add input="{HTTP_HOST}" pattern="lesterrassesdadrien.fr" />
</conditions>
<action type="Rewrite" url="http://lesterrassesdadrien_erp/erp/{R:1}" />
</rule>

IIS Rewrite conditions rule is not working

First thankyou anyone , Engilsh is not my native language
But this problem is very difficult for me
I need
http://xxxx.show.bis.tw/event_register/index.php?id=image01
to
http://xxxx.show.bis.tw/event_register/image01
and if the users enter http://xxxx.show.bis.tw/event_register/image01 on address bar
the website is can work
I think the rules is
users can input those
http://xxxx.show.bis.tw/event_register/image01
http://xxxx.show.bis.tw/event_register/index.php?id=image01
1.
http://xxxx.show.bis.tw/event_register/index.php?id=image01
Redirect like this http://xxxx.show.bis.tw/event_register/image01 and go to rule2
Rewrite <action type="Rewrite" url="index.php?id={C:1}" />
2.
http://xxxx.show.bis.tw/event_register/image01
just only Rewrite <action type="Rewrite" url="index.php?id={C:1}" />
my code is
if file and directory do Redirect or Rewrite website will broken (css/js/image)
I try to rule>1 and rule>2 write in one rule but it not work
I don't know how to fix it
<rewrite>
<rules>
// 1
<rule name="025" stopProcessing="true">
<match url=".*" />
<conditions> // file and directory not Redirect|Rewrite
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Redirect" url="{C:0}" appendQueryString="false" />
</rule>
// 2
<rule name="026">
<match url="index\.php(.*)" />
<conditions>
<add input="{QUERY_STRING}" pattern="id=(.*)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" />
</rule>
// 3
<rule name="027" enabled="true" stopProcessing="true">
<match url=".*" ignoreCase="true" />
<conditions>
<add input="{URL}" pattern="/index.php?id=" negate="true" />
</conditions>
<action type="Rewrite" url="index.php?id={R:1}" />
</rule>
</rules>
</rewrite>
thankyou anyone very thank
I almost tow days can't not sleep just fix this
Have you tried rewritemaps instead of rules in your web.config file?
<configuration>
<system.webServer>
<rewrite>
<rewriteMaps>
<rewriteMap name="rewrites">
<add key="/event_register/image01" value="/event_register/index.php?id=image01" />
</rewriteMap>
</rewriteMaps>
</rewrite>
</system.webServer>
</configuration>

HTTP Error 500.52 - URL Rewrite Module Error on Docker

I'm trying to rewrite all my URL's to HTTPS but getting this error, not sure what am I doing wrong:
Config Error
This configuration section cannot be used at this path. This happens
when the section is locked at a parent level. Locking is either by
default (overrideModeDefault="Deny"), or set explicitly by a location
tag with overrideMode="Deny" or the legacy allowOverride="false".
web.config:
<rewrite>
<allowedServerVariables>
<add name="HTTPS" />
<add name="X-FORWARDED-PROTO" />
</allowedServerVariables>
<rules>
<rule name="HTTPS_AlwaysOn" patternSyntax="Wildcard">
<match url="*" />
<serverVariables>
<set name="HTTPS" value="on" />
</serverVariables>
<action type="None" />
<conditions>
<add input="{HTTP_X_FORWARDED_PROTO}" pattern="https" />
</conditions>
</rule>
</rules>
</rewrite>
I have also installed Rewrite module and ARR on Docker container
# Install Url Rewrite
ADD https://download.microsoft.com/download/1/2/8/128E2E22-C1B9-44A4-BE2A-5859ED1D4592/rewrite_amd64_en-US.msi /install/rewrite_amd64_en-US.msi
RUN msiexec.exe /i c:\install\rewrite_amd64_en-US.msi /passive
ADD https://download.microsoft.com/download/A/D/C/ADC4BAF8-A094-47B5-A6F6-CE4C5ED18BF8/ARRv3_setup_amd64_en-us.EXE /install/ARRv3_setup_amd64_en-us.exe
RUN c:\install\ARRv3_setup_amd64_en-us.exe /Q
Build a normal IIS 10 machine for testing first and you should notice the same error, as that allowedServerVariables cannot be in web.config.
Reference
To enable SSL Rewrite simply add this inside:
<rules>
<rule name="SSL Redirect" enabled="true" stopProcessing="true">
<match url="(.*)"/>
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="off"/>
<add input="{HTTP_HOST}" pattern="localhost" negate="true"/>
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}"/>
</rule>
</rules>

IIS web.config : Rewrite URL file as subdomain

I am trying to rewrite my url with a web.config file.
Actual URL: sudomain.mydomain.com/legal.html
Wished URL: legal.mydomain.com
I have already hide the html extension.
<rule name="Hidehtml" enabled="true">
<match ignoreCase="true" url="Legal" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{URL}" pattern="(.*)\.(.*)" negate="true" />
</conditions>
<action type="Rewrite" url="Legal.html"/>
</rule>
I saw this kind of rewrite is possible with subfolder, but I wonder if it is possible with a file.
I am trying this one but it give me a DNS error.
<rule name="testrule" stopProcessing="true">
<match url="legal(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="mydomain.com" />
</conditions>
<action type="Redirect" url="http://legal.mydomain.com" />
</rule>
I wonder if I am not missing something, maybe a redirect is needed too?
Thanks for your help !
To achieve this,
I just registered two CNAME for my website.
FQDN:subdomain.mydomain.com
FQDN:legal.mydomain.com
Then I apply them to my IIS site.
Just make sure both of them can be accessed in web browser.
Then I apply following rewrite rules
<rules>
<rule name="testrule" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{URL}" pattern="^/legal\.[a-zA-Z]+" />
<add input="{HTTP_HOST}" pattern="subdomain.candy.com" />
</conditions>
<action type="Redirect" url="http://legal.candy.com" />
</rule>
<rule name="rewrite rule">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="legal.candy.com" />
<add input="{URL}" pattern="^(/)?$" />
</conditions>
<action type="Rewrite" url="http://subdomain.candy.com/legal.html" />
</rule>
</rules>
When you access sudomain.mydomain.com/legal.html, the request will be redirected to legal.mydomain.com/ and legal.mydomain.com will rewrite back to /legal.html for response content.
Please keep in mind that legal.mydomain.com is always required to be registered in DNS even you just use it as a mask.
If your website are forwarding the public internet, please remember to purchase domain for legal.domain.com as well.

IIS Rewrite even rewrites images/css/js - results in 404 not found

I am trying to rewrite the urls to make them search engine friendly
www.mydomain.com/qa/213/who-am-i
rewrites as
www.mydomain.com/qa/?qa=213/who-am-i
The below block works, but the problem is that the js/css/images urls inside the page are also rewritten. So the page looks for files like www.mydomain.com/qa/213/who-am-i/jquery.js which actually doesn't exist. So the page loads, but none of the css, .js and images work.
<rule name="CleanRouting" stopProcessing="true">
<match url="^qa/(.*)/(.*)$" ignoreCase="true" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="true" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="true" negate="true" />
</conditions>
<action type="Rewrite" url="qa/?qa={R:1}/{R:2}&{QUERY_STRING}" appendQueryString="false" />
</rule>
Please tell me how to fix this. I am using Asp.Net MVC (if it matters).
After several hours & days I finally found the correct IIS rewrite rule to be used for bypassing images/css/js etc files so that the page appear correctly.
The following rewrite rules must be added to your ASP.Net MVC project's Web.Config.
<!--Rewrite all paths containing a period like www.conceptworld.com/qa/453/who-am-i/qa-images/search.png to www.conceptworld.com/qa/qa-images/search.png -->
<rule name="RewriteFileUrls" stopProcessing="true">
<match url="^qa\/([^\/]*)\/([^\/]*)\/(.*[\.].*)$" ignoreCase="true" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="true" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="true" negate="true" />
</conditions>
<action type="Rewrite" url="qa/{R:2}/{R:3}" appendQueryString="false" />
</rule>
I am using the above rule plus the below 2 rules for the Question2Answer Question Answer PHP based solution similar to StackOverflow. I have installed Question2Answer on a Windows system with IIS + Asp.Net MVC.
<!--Rewrites urls like www.conceptworld.com/qa/tags -->
<rule name="RewriteSingleLevelUrls" stopProcessing="true">
<match url="^qa\/([^\/]*)$" ignoreCase="true" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="true" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="true" negate="true" />
</conditions>
<action type="Rewrite" url="qa/?qa={R:1}&{QUERY_STRING}" appendQueryString="false" />
</rule>
<!--Rewrites urls like www.conceptworld.com/qa/56/who-am-i -->
<rule name="RewriteTwoLevelUrls" stopProcessing="true">
<match url="^qa\/([^\/]*\/[^\/]*)$" ignoreCase="true" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="true" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="true" negate="true" />
</conditions>
<action type="Rewrite" url="qa/?qa={R:1}&{QUERY_STRING}" appendQueryString="false" />
</rule>
Just setting the above rules is not enough. What is most important is setting your base url in Question2Answer. I almost gave up and thought it was impossible to have pretty SEO friendly urls in Question2Answer when it is installed on Windows (IIS + Asp.Net MVC), until I discovered this setting. Thanks to Question2Answer developers :)
Go to Admin/Layout and set the base url like below:
Now you are all set to run Question2Answer along with IIS Asp.Net MVC.

Resources