I am totally new to IIS servers and need to implement some rewrites.
The following rule in my RewriteMaps file causes a 500 error:
<add key="/contenttemplates/news.aspx?id=8589976277&LangType=3081&CurrencyType=156" value="/" />
While this one works fine:
<add key="/offset-air.php" value="/" />
I can only assume the query string has something to do with the issue, but I struggle to find a reason as to why this URL would be an issue. Initial google attempts have come up empty.
I would be very grateful if someone could point me in the right direction.
Many Regards!
If your rewrite maps are causing you issues, you should consider encoding your urls correctly.
An & in the URL (in either the key or the value) should be replaced with an &.
<add key="/contenttemplates/news.aspx?id=8589976277&LangType=3081&CurrencyType=156" value="/" />
would then become:
<add key="/contenttemplates/news.aspx?id=8589976277&LangType=3081&CurrencyType=156" value="/" />
Related
I do my best to scan the forum for help to make a web.config to do a Rewrite of this kind of url
domain.com/default.asp?id=3&language=2
My hope is that this can be
domain.com/en/service
where language=2 is "en"
and id=3 is page "Service" (this name exist in a mySQL)
I can only find example that do it vice versa...
Like this
<rewrite>
<rules>
<rule name="enquiry" stopProcessing="true">
<match url="^enquiry$" />
<action type="Rewrite" url="/page.asp" />
</rule>
</rules>
</rewrite>
I would like it to be something like this... I know this isn't correct, but maybe explains my problem.
<rewrite>
<rules>
<rule name="enquiry" stopProcessing="true">
<match url="^default.asp?id=3&language=2$" />
<action type="Rewrite" url="/en/serice" />
</rule>
</rules>
</rewrite>
If you want to use regular expressions you could do something like this
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
<match url="^([^/]+)/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="default.asp?language={R:1}&id={R:2}" />
</rule>
This would rewrite "domain.com/en/service" as "domain.com/default.asp?language=en&id=Service", or "domain.com/2/3" as "domain.com/default.asp?language=2&id=3"
To change the 2 to en and the 3 to service, along with all the other options though I think you would need a separate rule for each permutation, or have some sort of logic within your asp pages to read your querystring variables and send the corresponding values to your SQL queries. Note also that the parameters in the friendly url appear in the same order and the querystring variables in the rewritten URL, although this shouldn't really be an issue. If someone tries to access the page with the original "unfriendly" url they will find what they are looking for, whichever way round they enter the querystring variables.
Please note, I didn't actually hand code the rule above, I generated it with the URL Rewrite module in IIS manager - it makes life a lot easier
Also note, as discussed with my namesake in the other answer, this only applies to IIS7 and above
I have done this in Classic ASP using custom error pages and this seems to be the best way unless you use some sort of third party component installed on the server.
To do this, in IIS (or web.config) you need to set up 404 errors to go to a specific custom error Classic ASP page (eg. 404.asp).
In this custom error page you first need to check to see if the URL is valid. If it is you can Server.Transfer to the correct page, return a 200 response code, and parse the URL there to convert the URL to the values needed for the database lookup, etc. If it's not a valid URL then you show a custom error page and return a 404 response code.
The code to check for a valid URL and to retrieve the URL parameters will vary greatly depending on your URL structure. But to find the URL requested on the custom 404 error page you have to look at the querystring, which will be something like "404;http://domain.com:80/en/service/".
Here's some sample code that gets the parameters from the requested URL:
Dim strUrl, intPos, strPath, strRoutes
strUrl = Request.ServerVariables("QUERY_STRING")
If Left(strUrl, 4) = "404;" Then
intPos = InStr(strUrl, "://")
strPath = Mid(strUrl, InStr(intPos, strUrl, "/") + 1)
If strPath <> "" Then
If Right(strPath, 1) = "/" Then strPath = Left(strPath, Len(strPath) - 1)
End If
strRoutes = Split(strPath, "/")
'Here you can check what parameters were passed in the url
'eg. strRoutes(0) will be "en", and strRoutes(1) will be "service"
End If
And here's how you can setup custom error pages in the web.config (rather than in IIS):
<?xml version="1.0"?>
<configuration>
<system.webServer>
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" subStatusCode="-1" responseMode="ExecuteURL" path="/404.asp" />
</httpErrors>
</system.webServer>
</configuration>
So I'm having an issue and not sure if it's even possible.
Here's the scenario. We utilize an F5 loadbalancer with an i-Rule set to send us a header (HTTP_IV-USER) value based on an access token.
We want to query that header, see if it matches a value we have setup in a rewritemap and then change it accordingly.
I haven't seen anyone doing this with servervariables. It makes sense on how to do it in regards to changing an URL... but we'd like to change a header variable.
Basically we're taking the value from the Token, which is a number, and then matching that number to a username in active directory.
Thanks for the help!
Of course, we can use rewritemap to check and replace the value. You could modify the rule below to achieve your requirement.
<rewriteMaps>
<rewriteMap name="StaticMap">
<add key="aaaaaaaaa" value="bbbbbbbb" />
</rewriteMap>
</rewriteMaps>
<outboundRules>
<rule name="rewritemaprule">
<match serverVariable="HTTP_IV-USER" pattern="(.*)" />
<conditions>
<add input="{StaticMap:{HTTP_IV-USER}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" value="{C:1}" />
</rule>
</outboundRules>
My source xml looks like this :
<connectionStrings>
<clear />
<add name="StrConn" providerName="SQLNCLI10"
connectionString="Server=dbserver;Database=db;User Id=user;Password=pass;" />
</connectionStrings>
Notice the seemingly innocent <clear /> tag.
Once I've imported this xml, and made changes to the xml file i.e: the connection string. All single tags in the document like <add /> or <clear /> are rewritten to long form eg: <clear></clear> and this prevents my service / app from even running.
It seems crazy since ultimately it seems like valid XML, but yeah it dies with an unknown fault exception, but when replacing the clear tags to all be <clear /> and not <clear ></clear> it works.
How can I prevent installshield from transforming these tags?
Are you using the 'update xml' feature? Try using the 'udpate text file' instead. That's a bit ugly to use for xml files but it works (we've been using it before the update xml feature was introduced)
I have a domain group with space in it, it does not work. The only related thing I find on google is this unanswered question
http://webclientguidance.codeplex.com/discussions/9242
<rules>
<add name="User" expression="R:MyDomain\MyApp Users" />
</rules>
I am getting this exception :
Found token "end of file" when expecting word at position 17.
The solution was painful simple
<add name="User" expression="R:"MyDomain\MyApp Users"" />
I imported rules from IIRF into IIS URL Rewrite, and most seemed to work fine. I just noticed tho that some urls have percent symbols in them (trying to redirect some bad inbound links, with percent encoded characters in them). The regex seems to not work when the percent is in there, so I assume it is trying to interpret is as a command or something. Can't find any documentation on this, anyone know?
The accepted answer didn't work in my case, but I discovered a different way to setup the rewrite rule. This will do a 301 redirect.
Requested URL: http://www.shuttercontractor.com/m/vinyl-%E2%80%8Bshutters.aspx
Target URL: http://www.shuttercontractor.com/m/vinyl-shutters.aspx
<rule name="301 Redirect to vinyl shutters category" stopProcessing="true">
<match url="." ignoreCase="false" />
<action type="Redirect" redirectType="Permanent" url="m/vinyl-shutters.aspx" />
<conditions>
<add input="{UNENCODED_URL}" pattern="m/vinyl-%[Ee]2%80%8[Bb]shutters\.aspx" ignoreCase="false" />
</conditions>
</rule>
Basically, the match will work on any URL, and we use a condition with the UNENCODED_URL server variable to ensure the pattern matches before redirecting.
appears that the rewrite rules already undo the url encoding, so it no longer sees a %3E as that, but instead as a '<'.. so using a > in place of %3E does the trick. Now, to go fix a bunch of urls. argh.
Edit:
Also, if you hand edit the web.config (versus using the UI editor), you will need to use & lt ; for the < symbols. It's probably best to use the UI to avoid confusion.