I have done a bunch of sites using URL Rewrite actions in the web.config with much success.
Today I am trying to do the following rewrite rule:
Example url: domainname.com/ANYTHINGHERE/PRODUCTID
I basically want to take the product ID and pass it to a page (i.e. product.aspx?id=PRODUCTID
<rule name="Rewrite Product Details Page" stopProcessing="true">
<match url="^([^/]+)/([^/]+)$" />
<action type="Rewrite" url="/product-view.aspx?id={R:1}" appendQueryString="true" logRewrittenUrl="true" />
</rule>
I've done this in the past just fine but it had to have a static root folder, for instance: domanname.com/products/ANYTHINGHERE/ID
EDIT: (forgot to describe my issue),
When I try to load my page - domain.com/categoryname/PRODUCTID
The product page is called by the ID is not passed in.
Any ideas? I searched and could not find anything.
-Joe
I think the index is not zero-based. Try
<rule name="Rewrite Product Details Page" stopProcessing="true">
<match url="^([^/]+)/([^/]+)$" />
<action type="Rewrite" url="/product-view.aspx?id={R:2}" appendQueryString="true" logRewrittenUrl="true" />
</rule>
Here is a good tutorial: Creating Rewrite Rules for the URL Rewrite Module
Related
When I installed Wordpress I decided on what I thought would be a decent URL and started writing blog posts and pointing people to www.moreGenericDomain.com/myblog/. Everything worked, and still works, fine. But as my blog focus was narrowed the domain didn't quite suit it anymore.
I've since purchased a new domain that I would like to use. When I go into settings and set www.NewDomain.com as the blog address everything works fine for that domain. Further, the urls are cleaner as they have changed from www.moreGenericDomain.com/myblog/post-1 to www.NewDomain.com/post-1. So everything seemed to go going great.
However, after this change www.moreGenericDomain.com/myblog/post-1 would give a Wordpress 404 page. This is very undesirable as I already have a number of links to my blog posts and a few initial subscribers. I don't want to lose this established traffic that I've worked so hard for.
Is it possible to switch to the new domain name while keeping the old one functional? In other words, I would like www.moreGenericDomain.com/myblog/post-1 to redirect to www.NewDomain.com/post-1.
Update
Per some helpful tips in the comments I tried setting up rewrite rules. My web.config looks like this:
<rewrite>
<rules>
<rule name="multAccounts">
<match url="http://oldDomain.com/LMBlog/post-1/" />
<action type="Rewrite" url="http://newDomain.com/post-1/" />
</rule>
<rule name="darkEssences">
<match url="http://oldDomain.com/LMBlog/post-2/" />
<action type="Rewrite" url="http://newDomain.com/post-2/" />
</rule>
<rule name="GuildBash">
<match url="http://oldDomain.com/LMBlog/post-3/" />
<action type="Rewrite" url="http://newDomain.com/post-3/" />
</rule>
<rule name="KvK">
<match url="http://oldDomain.com/LMBlog/post-4/" />
<action type="Rewrite" url="http://newDomain.com/post-4/" />
</rule>
</rules>
</rewrite>
The first rule seems to work correctly, while IIS ignores the other 3. I've used IIS' rule editor to test my input URL and it says it matches, but when I go to that URL I am not redirected and Wordpress gives me a 404.
I also tried a regex rule:
<rules>
<rule name="RegexRewrite">
<match url="([a-zA-Z\-\:\/]+)oldDomain.com\/lmblog\/([a-zA-Z\-]+)" />
<action type="Rewrite" url="http://NewDomain.com/{R:2}" />
</rule>
</rules>
However, that is completely ignored; no rewrites occur at all and I received the Wordpress 404 page. I suspect that Wordpress is preventing IIS from doing url rewrites.
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.
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.
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.