IIS rewrite rendering script, css and image URLs incorrectly - iis

I have a rewrite rule in my web.config like this:
<rules>
<rule name="products" enabled="true" stopProcessing="true" >
<match url="^products/([_0-9a-z-]+)/([_0-9a-z-]+)" />
<action type="Rewrite" url="test_produit.aspx?code_prod={R:1}&lib_prod={R:2}" />
<conditions logicalGrouping="MatchAny">
<add input="{URL}" pattern="^.*\.(ashx|axd|css|gif|png|jpg|svg|jpeg|js|flv|f4v)$" negate="true" />
</conditions>
</rule>
</rules>
I want to have url like this :
http://www.test.com/products/1502/nice-car/
The problem is that the links for my Javascript, CSS and images are broken and are rendered as:
http://www.test.com/products/1502/nice-car/css/...
or
http://www.test.com/products/1502/nice-car/js/...
...instead of rendering as:
http://www.test.com/css/...
or
http://www.test.com/js/...
How can I resolve this issue?

Related

URL Rewrite-rule in IIS 8 not triggering

I have a problem with my URL rewrite and I don't know what I'm doing wrong so, maybe you can point me in the right direction. We had a intranet-site, which had a pattern like this: intranet.old-site.com. Now we got a new domain and I want to forward my outdated links to the front-page of our new intranet, which looks like: intranet.new-site.com.
I installed the URL Rewrite module in IIS and in my point of view, the setup is correct:
Match URL
Request URL: matches pattern.
Using: Wildcards
Pattern: *intranet.old-site*
Ignore case: true
Conditions
none
Server Variables
none
Action
Action Type: Rewrite
Action Properties: Rewrite-URL: http://intranet.new-site.com/
Append query string: true
Log rewritten URL: false
Stop processing of subsequent rules: false
I'm also open for any idea what might work, so if there is a rule for the web.config, I could try that as well.
Update 1:
the web-config looks like this now:
<rewrite>
<rules>
<rule name="RewriteSG2DE" patternSyntax="ECMAScript" stopProcessing="true">
<match url="(.*).sgbdd.saint-gobain(.*)" />
<action type="Redirect" url="intranet.stark-deutschland.net" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="(.*)sgbdd.saint-gobain(.*)" />
<add input="{HTTP_HOST}" pattern="intranet.sgbdd.saint-gobain.com" />
</conditions>
</rule>
</rules>
</rewrite>
I want any site, which is basically anything like intranet.sgbdd.saint-gobain.com/start.asp?something_more" to be forwarded / redirected to intranet.stark-deutschland.net/start.asp?something_more
For instance:
intranet.sgbdd.saint-gobain.com/start.asp?something_more --> intranet.stark-deutschland.net/start.asp?something_more
somesite.intranet.saint-gobain.com/my/new/site --> somesite.intranet.stark-deutschland.net/my/new/site
I also added conditions as suggested:
Thanks for you input
Using two URL rewrite rules is much easier to handle this.
If you want to redirect
intranet.sgbdd.saint-gobain.com/start.asp?something_more --> intranet.stark-deutschland.net/start.asp?something_more
somesite.intranet.saint-gobain.com/my/new/site --> somesite.intranet.stark-deutschland.net/my/new/site
Then rule can be like this.
<rule name="rule1" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="true">
<add input="{HTTP_HOST}" pattern="^(.*)saint-gobain.com$" />
<add input="{HTTP_HOST}" pattern="intranet.sgbdd.saint-gobain.com" negate="true" />
</conditions>
<action type="Redirect" url="http://{C:1}stark-deutschland.net/{R:1}" redirectType="Temporary" />
</rule>
<rule name="rule2" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^intranet.sgbdd.saint-gobain.com$" />
</conditions>
<action type="Redirect" url="http://intranet.stark-deutschland.net/{R:1}" redirectType="Temporary" />
</rule>
I tried to combine two rules to one rule but there's something wrong with expression ?! so it is recommended to split into two rules.
If you means
intranet.sgbdd.saint-gobain.com/start.asp?something_more --> intranet.stark-deutschland.net/start.asp?something_more
somesite.intranet.sgbdd.saint-gobain.com/my/new/site --> somesite.intranet.stark-deutschland.net/my/new/site
Then the rule can be like this.
<rule name="RewriteSG2DE" patternSyntax="ECMAScript" stopProcessing="true">
<match url="(.*)" />
<action type="Redirect" url="http://{C:1}stark-deutschland.net/{R:1}" redirectType="Temporary" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="(.*)sgbdd.saint-gobain.com" />
</conditions>
</rule>

URL Rewriting the path in IIS

I have a site that uses two characters in the URL path to determine the initial language e.g. https://my.company.net/Monitoring/gb displays in the English language. I want to setup a redirect for these two display the full culture code e.g. https://my.company.net/Monitoring/en-GB
This is the rule that I've tried:
<rewrite>
<rules>
<rule name="Monitoring gb rewrite" patternSyntax="ExactMatch" stopProcessing="true">
<match url="https://my.company.net/Monitoring/gb" />
<conditions />
<serverVariables />
<action type="Redirect" url="https://my.company.net/Monitoring/en-GB" appendQueryString="false" />
</rule>
</rules>
</rewrite>
I expected https://my.company.net/Monitoring/gb to redirect to https://my.company.net/Monitoring/en-GB however this rule does not have any effect: the browser URL stays at https://my.company.net/Monitoring/gb.
How can I rectify this?
You could use below url rewrite rule.
<rule name="gb to en-gb redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="ww.sample1.com" />
<add input="{HTTPS}" pattern="on" />
<add input="{REQUEST_URI}" pattern="Monitoring/gb" />
</conditions>
<action type="Redirect" url="https://www.sample1.com/Monitoring/en-GB" />
</rule>
Note: use your hostname instead of the www.sample1.com.
The redirect failure was due to browser caching. The redirect works once the cache is cleared.

IIS 7.5 URL Rewrite Module - Rewrite Maps with variables

I have several pages on my site that have long and unreadable URLs that I'd like to be able shorten. The problem I'm having is appending query strings with variable values.
For example:
"www.example.com/dir1/dir2/filename.php" shortens to "www.example.com/file".
"www.example.com/dir1/dir2/filename.php?id=2" would be "www.example.com/file/2".
"www.example.com/dir1/dir2/filename.php?id=2&alt=6" would be "www.example.com/file/2/6".
The values of 'id' and 'alt' are then used by our page to access information in our database, which determines the contents of the page. These values can change, and there is no set amount.
Right now I've got the first example working fine using the following rewrite rules:
<rewrite>
<outboundRules>
<remove name="OutboundRewriteUserFriendlyURL1" />
</outboundRules>
<rewriteMaps>
<rewriteMap name="StaticRewrites">
<add key="/file" value="/dir1/dir2/filename.php" />
</rewriteMap>
</rewriteMaps>
<rules>
<rule name="Rewrite Rule">
<match url=".*" />
<conditions>
<add input="{StaticRewrites:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="{C:1}" />
</rule>
</rules>
</rewrite>
But I haven't been able to find anything that would allow the URLs to contain variables. Everything I've seen has used static rewrites like my current solution is using, and I can't find anything about allowing arbitrary parameters.
EDIT:
Found a better solution that doesn't use a Rewrite Map. I had attempted a simliar rule previously, but due to the IIS setup on our testing environment it wasn't working as expected. This version should work for most people.
<rule name="Curricula View" stopProcessing="true">
<match url="/file(?:/(\d+)(?:/(\d+))?)?" />
<action type="Rewrite" url="/dir1/dir2/filename.php?id={R:1}&alt={R:2}" appendQueryString="true" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
</rule>
Using this rule I was able to avoid using a rewrite map altogether. I had attempted to use something like this originally, but due to the setup of our test environment it wouldn't work without some weird tweaks. This version should work in all normal environments.
<rule name="Curricula View" stopProcessing="true">
<match url="/file(?:/(\d+)(?:/(\d+))?)?" />
<action type="Rewrite" url="/dir1/dir2/filename.php?id={R:1}&alt={R:2}" appendQueryString="true" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
</rule>
EDIT:
I was also able to get the original version with a rewrite map working for anyone interested.
<rule name="Rewrite Map with Variables" enabled="true">
<match url="^(.+?)/?/(.*)$" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{ProductMap:{R:1}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="/dir1/dir2/filename.php?id={C:0}&other={R:2}" appendQueryString="true" />
</rule>

URL Rewrite Module IIS 7 redirect for urls with colon not working

I am trying to redirect the content of an old website to a new website using IIS. My rewrite map looks like this:
<rewriteMaps>
<rewriteMap name="Redirects">
<add key="/q/R.208-9899.aspx" value="/bla/garden/childrens-gardening/cat3376588.cat"/>
<add key="/store/dvd/lego-the-adventures-of-clutch-powers/8%3a939022" value="/bla/entertainment-books/dvd/cat12590010.cat" />
<add key="/store/dvd/world-steam-today--north-central-and-south-america/8:470831" value="/bla/entertainment-books/dvd/cat12590010.cat" />
</rewriteMap>
</rewriteMaps>
and my web.config looks like this:
<rule name="Redirect rule1 for Redirects" enabled="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{Redirects:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="http://www.example.com{C:1}" appendQueryString="false" />
</rule>
The first url works fine but the second two fail to work.
Any pointers would be useful.

Htaccess and CakePHP 2 on windows IIS7

I was having problems trying to configure the mod rewrite over windows and II7:
CakePHP 2.2.2 not working on Windows IIS7 but finally i could import the htaccess in order to create the web.config file for IIS7.
The thing is:
Now that file has been created in cakephp folder and i can access to the main page BUT it has not been created inside app/ or app/webroot in which you can find 2 more .htaccess files.
Now, i can not access any other view than the main site, it shows a 404 page not found error and i am pretty sure that it is because it is not getting those .htaccess files on the web.config.
My cakephp web.config file looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^$" ignoreCase="false" />
<action type="Rewrite" url="app/webroot/" />
</rule>
<rule name="Imported Rule 2" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<action type="Rewrite" url="app/webroot/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Although the CakePHP documentation tells you to add a different code (which makes the views work but no styles are loaded. And the home page don't work.)
http://book.cakephp.org/2.0/en/installation/advanced-installation.html#url-rewrites-on-iis7-windows-hosts
When trying to access a CSS file from the URL i get this message:
Missing Controller
Error: CssController could not be found.
Error: Create the class CssController below in file: app\Controller\CssController.php
<?php
class CssController extends AppController {
}
Any idea? Working with Cakephp over windows is driving me crazy...
I should mention that my response is an improvement on the solution posted here: http://www2.palomar.edu/pages/sphillips/cakephp-with-iis-7-rewrite-rules-in-a-sub-folder/
A simpler and more flexible fix would be to get rid of /{Path_To_CakePHP_Directory}/ altogether, including the forward slash (/). By keeping the paths relative, your project folder becomes a lot more mobile. Here's what the web.config would look like:
<configuration>
<system.webServer>
<rewrite>
<rules>
<clear/>
<rule name="Imported Rule 0" stopProcessing="true">
<match url="^(img|css|files|js)(.*)$"></match>
<action type="Rewrite" url="app/webroot/{R:1}{R:2}" appendQueryString="false"></action>
</rule>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" url="index.php?url={R:1}" appendQueryString="true" />
</rule>
<rule name="Imported Rule 2" stopProcessing="true">
<match url="^$" ignoreCase="false" />
<action type="Rewrite" url="app/webroot/" />
</rule>
<rule name="Imported Rule 3" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<action type="Rewrite" url="app/webroot/{R:1}" />
</rule>
<rule name="Imported Rule 4" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" url="index.php?url={R:1}" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Ok, finally i get it work using this web.config i found on this site:
http://www2.palomar.edu/pages/sphillips/cakephp-with-iis-7-rewrite-rules-in-a-sub-folder/
I just changed the /{Path_To_CakePHP_Directory}/ for /.

Resources