Url Rewrite Drop File Extension But Keep Querystring For Specific Page - url-rewriting

I'm trying to match
https://www.example.com/abc/xyz.aspx?p1=a&p2=b&p3=c&p4=d
and I want to rewrite it to
https://www.example.com/abc/xyz?p1=a&p2=b&p3=c&p4=d
I've got this rewrite rule, but it's not working...
<rule name="Redirect Home Detail Pages">
<match url="(\/abc\/xyz.aspx).*" />
<action type="Redirect" url="/abc/xyz" appendQueryString="True" redirectType="Permanent" />
</rule>
Any suggestions?

It should be like that:
<rule name="Redirect Home Detail Pages">
<match url="^abc/xyz.aspx$" />
<action type="Redirect" url="/abc/xyz" appendQueryString="True" redirectType="Permanent" />
</rule>

Related

Need to redirect folder URL /media/default to Page URL /media/default/image

IIS rewrite rule is not working.
I have tried adding the following rule for redirect but it's not working. Can anyone help me on this as I am not familiar with rewrite rules.
<rule name="test" patternSyntax="ExactMatch" stopProcessing="true">
<match url='/media/default' />
<action type="Redirect" url="/media/default/image" />
<conditions>
</conditions>
</rule>

Rewrite rule for windows server + IIS and Cakephp 3.x

I want to deploy my site to Windows hosting from linux. On Linux server everything working as expected. but few things are not working on windows server.
Please let me know what changes i need to do in web.config
My file system is:
-src
-webroot
--css
--about.html
--index.php
--contact.php
Now how i can access about.html directly http://www.example.com/about.html and similarly for other php files too.
these are rule i am using for now:
<rewrite>
<rules>
<rule name="Exclude direct access to webroot/*"
stopProcessing="true">
<match url="^webroot/(.*)$" ignoreCase="false" />
<action type="None" />
</rule>
<rule name="Rewrite routed access to assets(img, css, files, js, favicon)"
stopProcessing="true">
<match url="^(assets|font|fonts|img|images|css|files|js|favicon.ico)(.*)$" />
<action type="Rewrite" url="webroot/{R:1}{R:2}"
appendQueryString="false" />
</rule>
<rule name="Rewrite requested file/folder to index.php"
stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<action type="Rewrite" url="index.php"
appendQueryString="true" />
</rule>
</rules>
</rewrite>

When a dot exist in the middle of the URL, the rewriter works wrong

I have a project in asp net core 2.0.
I Have stablished the next Rule in the web.config:
<rewrite>
<rules>
<rule name="wwwroot" >
<match url="([\S]+[.](js|css|png|gif|jpg|jpeg))" />
<action type="Rewrite" url="wwwroot/{R:1}" logRewrittenUrl="true" />
</rule>
</rules>
</rewrite>
When i acces to the url: www.foobar.com/lib/chart.js/dist/Chart.js for some reason the iis try to finde Chart.js in the path: wwwroot/lib/chart.js ignoring the last part of the url (/dist/Chart.js) when im expecting to take wwwroot/lib/chart.js/dist/Chart.js
Try to modify your regexp like this: ([\S]+[.](js|css|png|gif|jpg|jpeg))$ it should consider file extension if they at the end of your url
EDIT
To your rule you should exclude urls starting from wwwroot
<rule name="wwwroot" >
<match url="([\S]+[.](js|css|png|gif|jpg|jpeg))" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_URI}" negate="true" pattern="^/wwwroot" ignoreCase="true" />
</conditions>
<action type="Redirect" url="wwwroot/{R:1}" logRewrittenUrl="true" />
</rule>

IISRewrite encoding of %2f in path

So I have a path like this (I am using webforms):
folder/first/second/
which has the following rewrite rule behind it
<rule name="firstLevel">
<match url="^folder/([^/]+)/([^/]+)/?$" />
<action type="Rewrite" url="firstSecond.aspx?param1={R:1}&param2={UrlEncode:{R:2}}" />
</rule>
my issue is when I try and pass the following:
folder/first/sec%2fond/ (I want R2 to be sec%2fond where I will urlDecode it back to the page. for a result like "sec/ond".
However my rule keeps taking it as though I want
folder/first/sec/ond
Requested URL
http://localhost:85/research/first/sec/ond/
Physical Path
\rewritetest\folder\first\sec\ond\
Thanks for any assists..
I found a working solution to this issue for anyone else.
<rule name="secondLevel">
<match url="^folder/(.+)/(.+)/?$" ignoreCase="true" />
<action type="Rewrite" url="firstsecond.aspx?param1={C:1}&param2={C:2}" />
<conditions>
<add input="{UNENCODED_URL}" pattern="/folder/([^/]+)/([^/]+)/?$" />
</conditions>
</rule>

IIS URL rewrite - with dynamic files

I am using this answer https://stackoverflow.com/a/13861795/848513 which works great for static files, which no longer exist on my site, to forwards to new file locations.
But I also have some dynamic URL's that need to be forwarded to new locations, and the following code doesn't seem to work:
Works:
<rule name="SpecificRedirect50" stopProcessing="true">
<match url="aboutus.php" /> <!-- static URL -->
<action type="Redirect" url="/about-us" redirectType="Permanent" />
</rule>
Does not work:
<rule name="SpecificRedirect1" stopProcessing="true">
<match url="topic.php?id=39" /> <!-- dynamic URL-->
<action type="Redirect" url="/folder/?id=520" redirectType="Permanent" />
</rule>
The error i get when trying to go to www.site.com/topic.php?id=39 is a 404 Not Found error - ie, it isn't being filtered by the rewrite script.
What should the format be?
Thanks
OK, found answer - this format works:
<rule name="SpecificRedirect1111" stopProcessing="true">
<match url="^topic\.php$" />
<conditions>
<add input="{QUERY_STRING}" pattern="^id=39$" />
</conditions>
<action type="Redirect" url="/folder/?id=520" appendQueryString="false" redirectType="Permanent" />
</rule>

Resources