I am in the process of moving Blog from dasBlog to BlogEngine. In dasBlog, my URLs looks like this.
http://pfsolutions-mi.com/blog/2008/03/08/Beyond-Compare.aspx
Whereas in BlogEngine, my URLs looks like this.
http://pfsolutions-mi.com/blog/post/2008/03/08/Beyond-Compare.aspx
The only difference between the two URLs is the "post" sub-folder in BlogEngine.
Since I'm currently using IIS URL Rewrite to remove WWW from the URL, I figured the easiest solution would be to create another rule to handle adding the sub-folder. I tried something like this.
rule name="Blog Redirect" enabled="true" stopProcessing="true"
match url="^blog/([_0-9]+)/([_0-9]+)/([_0-9]+)/([_0-9a-z-]+).([_0-9a-z-]+)$"
action type="Redirect" url="blog/post/{R:1}/{R:2}/{R:3}/{R:4}.{R:5}" redirectType="Temporary"
However, when I enter an old dasBlog URL it does not get redirected to the new location. Instead I get the generic BlogEngine 404 Error page.
Note: I plan to change the redirectType to Permanent once I know everything is working.
Shouldn't your matching regex look more like this?
match url="^blog/([0-9]+)/([0-9]+)/([0-9]+)/([\w-]+)\.([a-z]+)$"
There are no underlines in date numbers anyway and your [_0-9a-z-]+ doesn't include uppercase letters like in "Beyond-Compare".
So here we should have: url="^blog/digits/digits/digits/any-word-characters.lowercase-letters$"
We could also specify more with:
match url="^blog/([0-9]{2,4})/([0-9]{2})/([0-9]{2})/([\w-]+)\.([a-z]{3,4})$"
Based on the assumption that you always have:
Year as "08" or "2008"
Month and day as "01" or "11"
File endings with 3 or 4 lowercase letters (htm, html, php, asp, aspx, etc.)
EDIT: I think "\w+" does not include hyphen, so you must turn this into "[\w-]+"
Related
I'm trying to write, hopefully, a single rewrite rule for a number of URLs that are being moved to a different structure. All of the URLs have a very similar structure so I'm thinking there must be a way to write a single rule for all of them.
Example:
http://example.com/some/deep/path/a/001/my-a-data
http://example.com/some/deep/path/a/002/my-a-data2
http://example.com/some/deep/path/b/001/my-b-data
http://example.com/some/deep/path/b/002/my-b-data2
http://example.com/some/deep/path/c/001/my-c-data
http://example.com/some/deep/path/c/002/my-c-data2
Need to redirect to (respectively):
http://example.com/a/my-a-data
http://example.com/a/my-a-data2
http://example.com/b/my-b-data
http://example.com/b/my-b-data2
http://example.com/c/my-c-data
http://example.com/c/my-c-data2
So what I'm trying to know is if there is a way to read the segments of the PATH in the URL so I can just reuse them to build the final URL because it is all there. It is just a simplification of the URL.
Perhaps to do something like
/{1}/{2}/{3}/{4}/{5}/{6}
http://example.com/some/deep/path/a/001/my-a-data -> http://example.com/{4}/{6}
Is that possible?
Thanks!!!
I found how it works.
Each segment in the URL path can be identified with {R} parameters in the rewrite rules. To match my URL structure I created the following rule:
<rule name="Documents redirection" stopProcessing="true">
<match url="^some/deep/path/(.*)/(.*)/(.*)$" ignoreCase="true" />
<action type="Redirect" url="/{R:1}/{R:3}" redirectType="Permanent" />
</rule>
Each of the (.*) segments are what the IIS Rewrite module recognizes as {R} parameter, so for my need, I needed the first and third parameter to form a new URL. I can safely ignore the second parameter.
I hope this helps someone else.
It is recommended to use([^/])+ instead of (.*) .
If there are multiple slash in your URL, the capture group would be corrupted.
For example:
Best regards,
Sam
I have recently found out that, if there are multiple URLs (eligible for rewriting) in one line of a web page, the IIS / ARR would only rewrite the first match of that line, and ignore the rest. So I'd like to ask two questions:
Is this the default behavior of the IIS / ARR URL rewriting function?
Is there any work-around for this behavior, such that the IIS / ARR could recognize -- and rewrite -- multiple URLs on the same line?
The solution for me was to ensure Regex doesn't match anything outside of the quotes, i.e. stop it from being greedy.
Using the GUI the match pattern ends with ([^"]*) instead of (.*), and when saved in the Web.config this gets escaped to ([^"]*)
The line in my Web.config looks like this:
<match filterByTags="None" pattern="http://your.domain/~/media([^"]*)" />
So I think it's safe to assume you're referring to an outbound rewrite rule to change URLs in HTML responses.
No that is not the default behaviour of the rewrite module.
Update your question to include your rule configuration so that we can help.
Useful information:
In your <match> element if you do not specify the filterByTags, then the match pattern will be applied on the entire response content, regardless of lines and occurrences. Note that the evaluation of regular expression patterns on the entire response content is a CPU intensive operation and may affect the performance of the web application.
It sounds to be that your rule(s) aren't properly configured.
Further information:
URL Rewrite Module 2.0 Configuration Reference
Creating Outbound Rules for URL Rewrite Module
I use IIS Rewrite Module. I have a few static files, and they are stored at
website/static/xyz/abc
I added IIS Rewrite rule as follows:
regular expression pattern:
xyz/.+
rewrite URL:
/static/{R:0}
First, if I type (with an ending /)
http://mysite/xyz/abc/
everything seems working just fine. The browser address bar shows exactly what I typed.
Second, If I type (without the ending /)
http://mysite/xyz/abc
the website still displays the content correctly, but the browser address bar changes to (by adding static and the ending /)
http://mysite/static/xyz/abc/
How can I make IIS not show /static in the second situation?
Thanks and regards.
UPDATE
Regular expression is used.
<rule name="inauguralball">
<match url="inauguralball(/.*)?" />
<action type="Rewrite" url="/static/{R:0}" />
</rule>
I think I found your answer here: http://support.microsoft.com/kb/298408/EN-US. When you leave out the trailing slash and IIS finds a directory that matches your request, it performs a redirect for you. I believe this is where your redirect is coming from, thereby sending you to /static/inauguralball/. Notice that IIS has also added the trailing slash for you because it detected that directory exists.
Can you make sure that your hrefs always point to /inauguralball/ (with the trailing slash) so that IIS doesn't perform that automatic redirect for you?
Seems like this would be really easy but can't seem to find an example.
User types in the following URL:
http://www.mysite.com/business/chelan/internet
I want it to go to:
/business/internet_chelan.php
ONLY if the file exists there. Otherwise, go to:
/business/internet.php
"Chelan" is a county in my area. If I want to create a specific page for that county, I could simply add "_chelan" to my file. Otherwise, it just goes to the general internet page.
So I use the following to grab URL parts:
^(business|residential)/([^/]+)/(.+)
and rewrite to:
{R:1}/{R:3}_{R:2}.php
Works beautifully... but I want to have the condition:
if {R:1}/{R:3}_{R:2}.php "Is a file" rewrite to {R:1}/{R:3}_{R:2}.php
But it seems like {R:} does not work in conditions??? Is this true or is there some other way I can write it? I've seen {C:1} and $1 but they don't seem to work either.
Any ideas?
IIS 7.5 on Windows 7 using URL Rewrite module
Using the URL Rewrite module, I've got a rule setup that is defined as:
Matches the pattern
Regular Expressions
Pattern: /support/viewmessages.aspx
Ignore Case: true
Action Type: Redirect
Redirect URL: http://newdomain/support/viewmessages.aspx
Append Query String: true
Redirect Type: 301
This sounds to me like it should redirect any of the URLs formated like:
http://olddomain/support/viewmessages.aspx?forum=20&topic=75942&ForumName=General%20Discussion
To be:
http://newdomain/support/viewmessages.aspx?forum=20&topic=75942&ForumName=General%20Discussion
However, nothing seems to be happening. I'm getting my 404 on the old domain, and it's not going to the 404 with the query string appended.
Here's the code that IIS generated in my web.config:
<rule name="Forum Posts" patternSyntax="ECMAScript" stopProcessing="true">
<match url="/support/viewmessages.aspx" />
<action type="Redirect" url="http://newdomain/support/viewmessages.aspx" appendQueryString="true" />
</rule>
Any help would be appreciated.
Attempt 1: Did you try the pattern without the forward slash? The other thought I'd have is whether there is something special to be done about the slash and dot since they may be seen as special characters within regular expressions.
Point 2: You do recognize that the dot is a special character in regular expressions, right? You may have to find a way to escape it so that the url likes like "/support/viewmatches\.aspx"