I'm working with an Umbraco asp.net website and I would like to ensure website url is alwayes displayed with www I have added:
<rule name="WWW rule" stopProcessing="false">
<match url=".*" />
<conditions>
<add input="{CACHE_URL}" pattern="^(.+)://(?!www)(.*)" />
</conditions>
<action type="Redirect" url="{C:1}://www.{C:2}" redirectType="Permanent" />
</rule>
To me this is a good methode for making sure only one URL on website. Only issue as I see is that when typing DOMAINE only my default.aspx will be added to frontpage.
How to avoid that?
I have tried to add:
<rule name="Default Document" stopProcessing="true">
<match url="(.*)default.aspx" />
<action type="Redirect" url="{R:1}" redirectType="Permanent" />
</rule>
This makes frontpage inside a loop....
Have I misunderstood somthing? Thanks
Shouldn't stopProcessing="true" for the domain rewrite? I'm guessing you don't want the rest of the rules evaluated?
Also, I'm guessing you might want to use {HTTP_HOST} in you condition, since I would guess that the {CACHE_URL} would be the one including the default.aspx path?
Currently you are using the incoming http/https schema, which are also considered different url's, so you may want to choose which one to use, or implement a canonical meta tag on your site to tell search engines which schema to use.
Related
I have to create a rewrite rule that catches all URLs from the old shop system and redirect them to the new shop. The problem is the following:
The source URL from the old shop can contain the path where you did find the product number, looks like this: www.domain.com/folder1/folder2/folder3/[product number].html
folder1, folder2 and folder3 can, but don't need to be in the called URL. They also are not fixed (e.g. folder1 can be "cars" or "bikes" or "services"). The link could also be just www.domain.com/folder1/[product number].html or even www.domain.com/[product number].html sometimes.
For the new system, I only need the [product number] in my target URL. Should look like this: www.domain.com/path1/path2/[product number].aspx
I could not find anything in Google or Stackoverflow that helped me with this.
Thanks in advance
Daniel
This should work:
<system.webServer>
<rewrite>
<rules>
<rule name="Three deep" stopProcessing="true">
<match url=".*/.*/.*/(.*?).html" />
<action type="Redirect" url="/path1/path2/{R:1}.aspx" />
</rule>
<rule name="Two deep" stopProcessing="true">
<match url=".*/.*/(.*?).html" />
<action type="Redirect" url="/path1/path2/{R:1}.aspx" />
</rule>
<rule name="One deep" stopProcessing="true">
<match url=".*/(.*?).html" />
<action type="Rewrite" url="/path1/path2/{R:1}.aspx" />
</rule>
<rule name="None deep" stopProcessing="true">
<match url="(.*?).html" />
<action type="Rewrite" url="/path1/path2/{R:1}.aspx" />
</rule>
</rules>
</rewrite>
</system.webServer>
I am worried that the expression - (.*?) - I'm using for the product code is too catch all, but I don't know the structure of your product codes. If you could tighten it up, it'd be better.
I'm also worried that they will hit things that aren't products (e.g. category and search) so if there is a pivot point that all product URLs use, I'd recomend using that. Tightening the product code bit will probably help false matches too.
At the moment it's working on the .html vs .aspx being the bit that isn't sending it into a redirect spiral.
I can probably do it in less rules with a better regex, which I'll have a bash at tonight.
ASP.NET 4.5 Webforms website
I have two domains:
company.com
company.co.ca
Both pointing to the same site and the default document is set to login.aspx.
When someone goes to company.co.ca, I check the Request.ServerVariables["HTTP_HOST"] in the login.aspx.cs and redirect users to company.co.ca/ca/login.aspx
I thought I can use the url rewrite and have this rule in my web.config but it doesn't redirect but stays on the main login.aspx.
<rewrite>
<rules>
<rule name="CA HomePage" stopProcessing="true">
<match url="company.co.ca/login.aspx" />
<action type="Redirect" url="company.co.ca/ca/login.aspx" />
</rule>
</rules>
</rewrite>
What am I doing wrong? Is there a better way?
We are planning to add one more domain and so I don't want to do a manual check in login.aspx.cs and redirect and want to find a more general best way.
You check for the host like this with IIS rewrites:
<rule name="CA HomePage" stopProcessing="true">
<match url="^login.aspx$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^company.com$" />
</conditions>
<action type="Redirect" url="http://company.co.ca/ca/login.aspx" />
</rule>
I'm attempting to write a rewrite rule for the following scenario.
User attempts to load this picture:
domain.com/images/folder/picture.jpg
and instead, I need it to load:
cdn.domain.com/images/folder/picture.jpg.
Here's what I have that isn't working:
<rule name="CDN rewrite for Images">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="domain.com" />
<add input="{REQUEST_URI}" pattern="^/images/folder/(.*)$" />
</conditions>
<action type="Rewrite" url="cdn.domain.com/images/folder/{C:1}" />
</rule>
UPDATE: Adding additional info. Most pictures are being served up from Joomla so while the root of the domain is something like domain.com, most images are input with a src="/images/folder/picture.jpg" Not quite sure how this is affecting the rewrite, but none of the options on cheesemacfly's answer below, are working...
UPDATE2: While cheesemacfly was unable to help me in my particular circumstances, I awarded him the bounty and marked his answer as the accepted one because he went above and beyond to try to help me in chat. Hopefully his answer will help someone with rewrites on IIS.
EDIT:
To be able to rewrite (and not only redirect) urls to outside websites, you need to install the Application Request Routing module and enable the proxy mode.
To do so:
Download and install the module
Open your IIS management console (inetmgr)
Select Your server node
Double click on Application Request Routing Cache:
Click on Server Proxy Settings on the Actions pane (right of the screen)
Check the box Enable proxy and click on Apply
The second step is about setting up your rules.
If you want your rewrite to be based on the path then use the following code:
<rewrite>
<rules>
<rule name="Rewrite to cdn domain">
<match url="^images/folder/(.+)$" />
<action type="Rewrite" url="http://cdn.domain.com/images/folder/{R:1}" />
</rule>
</rules>
</rewrite>
Or if you keep the same folder architecture on the second website you can simplify as follow:
<rewrite>
<rules>
<rule name="Rewrite to cdn domain">
<match url="^images/folder/(.+)$" />
<action type="Rewrite" url="http://cdn.domain.com/{R:0}" />
</rule>
</rules>
</rewrite>
If you want to catch only the files ending with a specific extension (let's say images):
<rewrite>
<rules>
<rule name="Forward to cdn domain">
<match url="^images/folder/.+\.(?:jpg|bmp|gif)$" />
<action type="Rewrite" url="http://cdn.domain.com/{R:0}" />
</rule>
</rules>
</rewrite>
Please refer to: http://www.iis.net/learn/extensions/url-rewrite-module/iis-url-rewriting-and-aspnet-routing (section "Which Option Should You Use?")
TIP:
The best way to test your pattern is to use the IIS test pattern tool.
At the root of your website -> URL Rewrite -> Create a blank rule -> click on test pattern:
If you don't get the expected result, you can debug your rewrite using the Failed Request Tracing tool
NOTE: Changing the rule to be a redirect instead of a rewrite fixes the problem. Ideally you want it to be a redirect but I have spent many hours trying to get the rewrite to work, and so far no solutions yet.
<rule name="Rewrite to images.cdn.com" enabled="true" stopProcessing="true">
<match url="images/(.+)$" ignoreCase="true" />
<action type="Redirect" url="http://images.cdn.com/{R:1}" />
</rule>
I am using URL rewrite to make user-friendly URL's. So www.mysite.som/search is being handled by www.mysite/search.aspx correctly. But there is one more thing I want. I want to prevent direct access to my page search.aspx. So, if someone enters www.mysite.com/search.aspx, he should be redirected to www.mysite.com/search instead.
What have I tried?
<rewrite>
<rule name="Redirect to friendly" stopProcessing="true">
<match url="^(.*)\.aspx$" />
<action type="Redirect" url="{R:1}" />
</rule>
</rewrite>
But its not working.
My company converted from an old website to a new one and we have a bunch of old pages with URLs like this:
www.example.com?foo.aspx
www.example.com?foo.aspx?ID=B&utm_source=Foo
www.example.com?foo.aspx?ID=C&utm_source=Foo
Those URLs need to go to these pages respectively:
www.example.com/ProductA
www.example.com/ProductB?utm_source=Foo
www.example.com/ProductC?utm_source=Foo
I can get this to work by using in my web.config but there are so many I would prefer to do it in the database. I have been able to partially successfully switch to the database using the article http://learn.iis.net/page.aspx/803/using-custom-rewrite-providers-with-url-rewrite-module/.
My issue is that all of my initially examples redirect to www.example.com/ProductA. It is as if they are ignoring the Query Strings. Any idea how to fix this? My rule in my config file is:
<rule name="DbProviderTest" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{DB:{R:1}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" />
</rule>
the URL that is matched in the tag does not include the Query String and that is why you will not see it in your R:1, you should be able to change your condition to be something like:
<add input="{DB:{R:1}?{QUERY_STRING}}" pattern="(.+)" />