web.config rewrite rule in Child folder - url-rewriting

I've a simple site with a web.config file for rewriting URL's
for instance, navigating to www.domain.com/story will show the page at www.domain.com/story.php
I've added a child folder with a copy of the site with a Chinese translation to www.domain.com/zh
I can access the story page at www.domain.com/zh/story.php but not www.domain.com/zh/story
How do I need to configure my web.config file? I've tried copying the web.config file to the /zh folder but this doesn't work, I've also tried copying and changing my match URL from ^story to ^zh/story but this also doesn't have the desired effect. Can someone suggest what is going on please?
Here is my existing web.config in the web root:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Rewrite to story.php">
<match url="^story" />
<action type="Rewrite" url="story.php" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Haven't had a chance to try myself, but try something like this:
<rule name="Rewrite to story.php">
<match url="^zh/story" />
<action type="Rewrite" url="zh/story.php" />
</rule>

Related

Point subdomain to it folder IIS

There's a way to point all my subdomains to the same name folder, using Windows Server | IIS - Rewrite Rule or Proxy?
Example:
subdomain1.domain.com ----> C:\customers\development\subdomain1
subdomain2.domain.com ----> C:\customers\development\subdomain2
subdomain3.domain.com ----> C:\customers\development\subdomain3
Is it possible do automatically?
I've tryied these web.config rules
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="rewrite" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(?!www\.)(.*)\example\.com$" />
</conditions>
<action type="Rewrite" url="/{C:1}/" appendQueryString="true" logRewrittenUrl="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Thanks in advance!
URL rewrite is mainly used to rewrite URL not physical path. So you have to create a web app with root folder C:\customers\development and bind all subdomains to this IIS site. Then modify your rule to
<rule name="rewrite" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(?!www\.)(.*)\.example\.com$" />
</conditions>
<action type="Rewrite" url="/{C:1}/{REQUEST_URI}" appendQueryString="true" logRewrittenUrl="true" />
</rule>
Please modify ^(?!www\.)(.*)\example\.com$ to ^(?!www\.)(.*)\.example\.com$.
Finally, you should be point subdomain to subfolder
Besides, if the application in folder domain1/domain2 and domain3 are three independent projects then you need to use sub-application instead of a folder.

web.config using an absolute path

I need to use a rewrite rule for a url(www.example.org/download.php) to hit another file called download.php, that is not in my root directory, but located in C:/repo/other_directory/test/download.php, however, it is not working. I have to use an absolute path in the rule. Any help on this would be greatly appreciated. My code is below:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Imported Rule placestoswim" stopProcessing="true">
<match url="download.php" ignoreCase="false" />
<action type="Rewrite" url="C:/repo/other_directory/test/download.php" appendQueryString="false" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
I dont think this will work the url paramter of a rewrite is expecting a URL not a file system path.
If you cant move that location, can you create a virtual directory inside your existing site called downloads, and point its web root at your immovable folder C:/repo/other_directory/test/? Then in your web.config setup a rewrite to use this newly created path ~/downloads/download.php as the destination for your rewrite for ~/downloads.php

URL rewrite - odd url is produced

My first attempt at doing an URL rewrite.
Usual scenario. Domain name seperate from host.
So I've changed the name servers and domain points to holding page at host so that's ok.
And purchased a domain pointer from the host to allow for a domain to be pointed at a sub folder.
Added the following code to web config file in root folder:
<?xml version="1.0"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="xyz.uk" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www.)?xyz.uk" />
</conditions>
<action type="Redirect" url="\xyz\ {R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
I get "The page isn't redirecting properly" and my url reads as "http://www.xyz.uk/%5Cxyz%5Cxyz/xyz/xyz/xyz/xyz/xyz/xyz/xyz/xyz/xyz/xyz/xyz/xyz/xyz/xyz/xyz/xyz/xyz/xyz/"
Googled this and all the examples I've seen are the same as the one I'm using.
Where am I going wrong?
Thanks in advance.
The actual code that works is below.
TIP.............I was helped on another forum and even though I made the changes, the page still wouldn't redirect.
PROBLEM WAS..........the browser cache was remembering the old redirect url!!
CLEAR DOWN your browser cache and any changes can then be seen immediately. Happened in Chrome and Firefox.
Correct code:
<?xml version="1.0"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="xyz.uk" stopProcessing="true">
<match url=".*" ignoreCase="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www.)?xyz.uk" />
<add input="{PATH_INFO}" pattern="^/xyz/" negate="true" />
</conditions>
<action type="Redirect" url="xyz/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

web.config transform not working when publishing

I have a solution with multiple projects, the transforms work in other projects but not this one. What could be the reason?
The project is an MVC 4 application
The configuration manager for the solution shows that when the solution configuration is LIVE then it should use the LIVE configuration for the project.
There is a Web.config, Web.Live.config, Web.Stage.config
In the Web.Live.config I have this code:
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<system.web>
<compilation debug="false" xdt:Transform="SetAttributes"></compilation>
</system.web>
<system.webServer>
<rewrite>
<rules>
<clear />
<rule name="Redirect to https" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
I publish it to my web server, ensuring the configuration is set to Live.
The web config on the server does not contain the transformations.
Any ideas?
I've solved this, it seems in this situation I had to add xdt:Transform="Insert" to the <rewrite> tag.
I don't know why I need that considering the default template for a transformed config file contains markup that inserts code without such an attribute that works ok...

ASP.NET 4.0 Rewrite Rules in Global.aspx File Issues with rewrite

I have a rewrite rule I'm using in ASP.NET 4.0 on IIS7:
<system.webServer>
<rewrite>
<rules>
<rule name="Rewrite default to aspx" stopProcessing="true">
<match url="^$" ignoreCase="false" />
<action type="Rewrite" url="home.aspx" />
</rule>
</rules>
</rewrite>
<defaultDocument>
<files>
<add value="home.aspx" />
</files>
</defaultDocument>
</system.webServer>
This rule takes: (http:/example.com/aboutus.aspx) and it removes the .aspx from the end of the URL. I'm running into problems with wordpress being installed on my subdomain (http:/www.example.com/blog) I get the following error due to my rewrite rule:
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /blog/.aspx
Does anyone know how I can fix the URL rewrite rule so it safely get to (http://www.example.com/blog/) and not add the .aspx at the end?
Is there some sort of syntax that can search for the blog subdirectory to ignore the directory '/blog/'?
Greatly appreciated! Thank you! :)
If you want your rule to be applied to every urls but the /blog/* ones, then you can use the negate option:
<rule name="Rewrite default to aspx" stopProcessing="true">
<match url="^blog/" ignoreCase="false" negate="true" />
<action type="Rewrite" url="home.aspx" />
</rule>
http://www.iis.net/learn/extensions/url-rewrite-module/url-rewrite-module-configuration-reference#Rule_pattern_properties

Resources