I've been given several requests to rewrite a URL in IIS. One of them includes having the .html page as part of the URL, but without the HTML tag. For example, www.foo.com/bar.html would be displayed as www.foo.com/bar instead. I have this rule to accomplish that and it seems to work ok.
<rule name="Hide .html ext" enabled="true">
<match url="^(.*)" ignoreCase="false" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}.html" matchType="IsFile" />
</conditions>
<action type="Rewrite" url="{R:0}.html" />
</rule>
The problem I'm having is that since index.html is a default document in IIS, so www.foo.com never shows index.html for my html rewrite rule to handle it (at least that is my assumption as to what is happening). This is the same if the page is something like www.foo.com/bar/index.html. They would want www.foo.com/bar to redirect to www.foo.com/bar/index in the URL.
Essentially is it possible to somehow force the default document to show? I've been attempting all sorts of strange scenarios using {PATH_INFO}, {URL}, and {PATH_TRANSLATED} to somehow attempt to catch any index.html page to rewrite it "correctly" in the URL.
Example attempt that I know is in the wrong direction:
<rule name="Include /index" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{PATH_INFO}" pattern="(.+?(?=(index.html|\/index.html|\/index.html\/)$)|^(index.html))" />
<add input="{URL}" pattern="(.+?(?=(index|\/index|\/index\/)$)|^((index)|(\/index\/)))" negate="true" />
</conditions>
<action type="Redirect" url="/index" appendQueryString="false" redirectType="Permanent" />
</rule>
I feel like I'm overthinking this and there's possibly a simpler solution that uses IIS URL rewrite.
If the default document function is enabled, the index cannot be displayed in the URL, including index.html.
If you want to display the index in the url unless manually enter it in the browser. There are some problems with URL rewriting. Even if the URI is empty, the request is sent to IIS, and what is received in IIS is the URL with farvicon.ico attached. In URL rewriting, it is not very accurate to determine whether the URI is empty.
Related
I am trying to pass two parameters in the URL with the following url rewrite rule and the page keeps showing 404 error.
Actual URL: https://devbox.mysite.com/kb/article/?slug=view-my-class-schedul&role=56c4cfe091121c0b5b47fe66
I want to show URL on browser: https://devbox.mysite.com/kb/article/view-my-class-schedule
Current URL Rewrite rule that's not working:
<rule name="KB-rewrite" enabled="true">
<match url="^kb/article/([a-zA-Z0-9\-]+)&role=([a-zA-Z0-9\-]+)$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{QUERY_STRING}" pattern="1" negate="true" />
</conditions>
<action type="Rewrite" url="kb/article/?slug={R:1}&role={R:2}" appendQueryString="false" />
</rule>
Okay, I think I figured it out. The display URL has to be something like: https://devbox.mysite.com/kb/article/view-my-class-schedule/reader. I guess the way I was thinking is not possible.
I have an IIS server with many domains multihomed. I have base rewrite rules for seo friendly URLS.
Example:
www.something.com/here-is-the-page
current converts to
www.something.com/index.htm?pagetitle=here-is-the-page
Problem is that one domain needs this rule to allow the URLs to read with .htm on the end.
Example:
www.something.com/here-is-the-page.htm
needs to convert to
www.something.com/index.htm?pagetitle=here-is-the-page
(note, just removed the .htm, although I still want the .htm to display in the location bar)...
Thanks everyone for all of the assistance...[sarcasm] below is the solution.
The key here is the MatchAll.
Look at the URL
Check to see if this is for www.something.com
Check to make sure there is not an actual file with the filename.htm
Check there is no directory with that name
If the file is sitemap.htm (which does exist, then do not process) This should be caught by the previous check for file, but just got added for overkill. Since this is a rewrite and not a redirect the .htm will remain displaying in the navigation bar.
<rule name="remove the htm" stopProcessing="true">
<match url="([_0-9a-z-]+)\.htm$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^(www.something.com)$" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" pattern="sitemap\.htm$" negate="true" />
</conditions>
<action type="Rewrite" url="/index.cfm?pagetitle={R:1}" />
</rule>
i've created friendly URLs for a website using the web.config file in IIS 7.5.
i've created a rule for an articles page which looks like this :
<rule name="RedirectUserFriendlyURL10" stopProcessing="true">
<match url="^articles\.php$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^title=([^=&]+)&id=([^=&]+)$" />
</conditions>
<action type="Redirect" url="articles/{C:1}/{C:2}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL10" stopProcessing="true">
<match url="^articles/([^/]+)/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="articles.php?title={R:1}&id={R:2}" />
</rule>
this works fine.
an example of the friendly URL would look like this : http://www.example.com/articles/name_of_article/2104
now my client wants to add a 'landing page' which will list all of the articles, rather than going directly to an individual article.
they want the link to read http://www.example.com/articles or something very similar.
now i've changed the code on the page articles.php so that if there is no id passed in the querystring, or if the querystring id is 0 then the page will display the article list as required... if there is an id it will show only that specific article.
my question is, how can i write a rule so that it will rewrite the URL correctly
e.g.
http://www.example.com/articles.php?id=2104&title=name_of_article
becomes
http://www.example.com/articles/name_of_article/2104
and
http://www.example.com/articles.php?id=0
becomes
http://www.example.com/articles
only the first part currently works using the rule shown above.
For local dev testing, I need to catch all requests to www.somedomain.com/XXX (where X is the path), and send them on to localhost/somevdir/XXX.
I've added this to my HOSTS file (c:\windows\system32\drivers\etc):
127.0.0.1 www.mydomain.com
Then, in IIS8 (Windows 8), I've added a binding to my "Default Web Site" for the host www.mydomain.com. This works, I can now browse www.mydomain.com/test.html and see a test html page. My virtual dir is inside the Default web site. I then add a URL Rewrite URL to the web site for the last bit:
<rewrite>
<rules>
<rule name="mydomain.com" stopProcessing="true">
<match url="^www.mydomain.com/(.*)" />
<action type="Rewrite" url="localhost/MyVDir/{R:1}" logRewrittenUrl="true" />
</rule>
</rules>
</rewrite>
But - that doesn't work. I get a 404 so it looks the the match never happens. I've tried redirect and rewrite, and I've tried without the ^ in the regex and a few other regex tweaks. Can someone explain what I've done wrong?
I think the following should work:
<rule name="mydomain.com" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^(mydomain\.com|www\.mydomain\.com)$" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="http://localhost/MyVDir/{R:1}" redirectType="Temporary" />
</rule>
The match on any URL makes sure the conditions are checked, and the HTTP_HOST server variable seems the most reliable way of checking the requested hostname. You could remove the REQUEST_FILENAME input condition, but it works as quite a nice sanity check to make sure static files are always served.
The following is better for catching both www. and non-www. versions of the domain so that you don't have to write the domain twice, which could possibly cause errors with being twice as likely to write a typo. (The parenthesis with the ? character means optional in regex terms.)
<rule name="mydomain.com" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^(www.)?mydomain\.com$" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="http://localhost/MyVDir/{R:1}" redirectType="Temporary" />
</rule>
I have been scratching my head with this problem for at least two months. I have a classified ads section in our site which has SEO friendly URL's I want to rewrite these url's. I am not sure if the problem is related to the subdomain for each region. The URL may look like this:
These are working URL's:
http://milwaukee.storeboard.com/classifieds/for-sale/business/make-incredible-income-while-growing-your-local-economy/425
I want to make it goto this address on our server:
http://milwaukee.storeboard.com/classifieds/viewad.asp?ClassAdID=425
I am using the following code in the web.config file... but no matter what variations I try I cant get it to work..
<rule name="Classifieds Ad" stopProcessing="true">
<match url="^http://([^/]+)/classifieds/.*?(\d+)$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="http://{R:1}/classifieds/viewad.asp?ClassAdID={R:2}" />
The only reason the first link works at this time is because I am using my 404 error page to process the request and then calling the page via MSXML2.ServerXMLHTTP.6.0 but this is unreliable and can timeout, especially during times of heavy usage. I want to remove this method and use the quick IIS URL Rewrite.
I would really appreciate any help you can provide
Try the below rule
<rule name="Classifieds Ad" stopProcessing="true">
<match url="^classifieds/.*?/(\d+)$" />
<conditions>
<add input="{HTTP_HOST}" type=”Pattern” pattern="^(.*)$">
</conditions>
<action type="Rewrite" url="{C:0}/classifieds/viewad.asp?ClassAdID={R:1}" />