URL Rewriting - removing the entire page name - asp.net

Currently I have a rewrite rule in my web.config that removes the ".aspx" page extension.
I want to rewrite my URL to remove the whole page name "page1.aspx" so my URL will show as "www.mysite.com/folder/" and not "www.mysite.com/folder/page1"
My rewrite code in the web.config I currently use is the following :
<rewrite>
<rules>
<clear />
<rule name="Redirect to clean URL" enabled="true" stopProcessing="true">
<match url="^([._0-9a-z-/]+).aspx$" ignoreCase="true" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Redirect" url="{R:1}" />
</rule>
<rule name="RewriteASPX" enabled="true">
<match url="(.*)" />
<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="{R:1}.aspx" />
</rule>
</rules>
</rewrite>
What do I need to add or edit to my current rules to get the results I need?
Thanks!

Quickly testing it on regex101.com, does this work?
<match url="^([._0-9a-z-/]+)/(.+).aspx$" ignoreCase="true" />
and
<action type="Redirect" url="{R:0}/" />
Adding the "/(.+)" makes sure the last word before aspx gets removed from the first match (R:0). R:1 will contain that word: "page1" while R:0 will contain:"www.mysite.com/folder".

Related

Webconfig URL Rewrite to Hide Classic ASP ext but not .aspx or other ext

I have been able to hide the .asp extension, but it also removes any other extensions and points to .asp
I know this is just a config issue but have no experience of the webconfig file configuration and wonder if anyone has a quick solution to save me a few hrs!
Code as I have it below from part of webconfig file
<rewrite>
<rules>
<rule name="Hide .asp Ext">
<match url="^(.*)$" ignoreCase="true" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}.asp" matchType="IsFile" />
</conditions>
<action type="Rewrite" url="{R:0}.asp" logRewrittenUrl="true" />
</rule>
<rule name="Redirect .asp Ext" stopProcessing="true">
<match url="^(.*).asp" ignoreCase="true" />
<conditions logicalGrouping="MatchAny">
<add input="{URL}" pattern="(.*).asp" />
</conditions>
<action type="Redirect" url="{R:1}" />
</rule>
</rules>
</rewrite>
Seems to me the first rule converts everything to an .asp extension first?
Try removing the first rule.
Try this? Seems to work on my test server.
<rule name ="redirect .asp to none" stopProcessing="true">
<match url="(.*)\.asp$" />
<action type="Redirect" url="{R:1}" redirectType="Permanent" />
</rule>
<rule name="hide .asp extension" stopProcessing="false">
<match url="(.*)" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="{R:1}.asp" />
</rule>
You have two rules defined. It looks like the first matches on all requests and "rewrites" them to have a .asp extension. The second matches on on .asp and then performs a redirect of some kind.
What is not clear from your question is how you want to 'hide' these files. If you want to deny all requests to .asp you would be much better served adding a Request Filtering "File Extensions" https://learn.microsoft.com/en-us/iis/configuration/system.webserver/security/requestfiltering/fileextensions/index

IIS web.config redirect rule for specific URL but not others

I want to add a redirect rule to the web.config file http://myurl.com/path/to/old
to http://myurl.com/path/to/new
but http://myurl.com/fr/path/to/old
http://myurl.com/cn/path/to/old
should stay the same. how can i achieve that with match/rule?
Add this to your web.config:
<rewrite>
<rules>
<rule name="Redirect old to new" stopProcessing="true">
<match url="^path/to/old" ignoreCase="false" />
<conditions>
<add input="{URL}" pattern="^fr/.*" ignoreCase="false" negate="true" />
<add input="{URL}" pattern="^cn/.*" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="/path/to/new" />
</rule>
</rules>
</rewrite>

IIS Rewrite Rule - How to include root documents

I have a simple IIS rule to redirect HTTPS to HTTP:
<rule name="HTTPS" enabled="true" stopProcessing="true">
<match url=".*\.(asp)$" ignoreCase="false" />
<conditions>
<add input="{HTTPS}" pattern="on" />
<add input="{REQUEST_URI}" negate="true" pattern="^/ecards/user*" ignoreCase="true" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
The matching url will only work on .asp files, but how can I also get it to work for root directories?
E.g.
example.com
example.com/
example.com/test
example.com/test/
I don't want to just have the match URL as:
<match url="(.*)" />
Because then other non .asp files get rewritten.
One way to include directories in your rule is to exclude everything else.
Assuming that everything else has a file extension (e.g. .php, .css. .js, etc.) you can negate all input that has the . in the path.
I changed your code a bit to make a working demo locally (I don't have HTTPS locally to test so instead of redirecting to HTTP I set it to redirect to About.aspx) and the two rules are:
<rule name="HTTPS" enabled="true" stopProcessing="true">
<match url=".*\.(asp)$" ignoreCase="false" />
<action type="Redirect" url="/About.aspx" appendQueryString="true" redirectType="Permanent" />
</rule>
<rule name="NEWRULE" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{REQUEST_URI}" negate="true" pattern=".*\.(.)$" ignoreCase="true" />
<add input="{REQUEST_URI}" negate="true" pattern="^/About*" ignoreCase="true" />
</conditions>
<action type="Redirect" url="/About.aspx" appendQueryString="true" redirectType="Permanent" />
</rule>
So, based on your original code sample, a new rule that will work for you would be similar to this:
<rule name="IncludeDirectories" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{REQUEST_URI}" negate="true" pattern=".*\.(.)$" ignoreCase="true" />
<add input="{HTTPS}" pattern="on" />
<add input="{REQUEST_URI}" negate="true" pattern="^/ecards/user*" ignoreCase="true" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
Note: The above approach is rather aggressive. You could replace this condition:
<add input="{REQUEST_URI}" negate="true" pattern=".*\.(.)$" ignoreCase="true" />
with the following:
<add input="{REQUEST_URI}" negate="true" pattern=".*\.(php|css|js|jpg|gif|png)$" ignoreCase="true" />
Where you exclude specific extensions. You add as many as you want.
Edit: If you want to have specific pages still with HTTPS maybe the following rule will be helpful (haven't tested it though). The previous rule sends to HTTP all URLs except those that have /ecards/user where this one sends to HTTPS those that have /ecards/user. I believe there will be no conflict.
<rule name="HTTPS2Admins" enabled="true" stopProcessing="true">
<match url="/ecards/user(.*)" />
<conditions>
<add input="{HTTP}" pattern="on" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/ecards/user{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>

Convert URL to lower case only for ASPX and extension less URLs

I am using this rule in IIS 7
<rule name="Convert to lower case" enabled="true" stopProcessing="true">
<match url=".*[A-Z].*" ignoreCase="false" />
<conditions>
<add input="{URL}" pattern="(.*)/admin/*" negate="true" />
</conditions>
<action type="Redirect" url="{ToLower:{R:0}}" redirectType="Permanent" />
</rule>
How do I modify it so that it only redirects the urls that the user is likely to see in the browser like /MyPage.aspx and /MyPage and perhaps /MyPage.htmL
EDIT: I ended up using this: (this solves problem with DotNetNuke and reduces unnecessary redirects)
<rule name="Convert to lower case" enabled="true" stopProcessing="true">
<match url=".*[A-Z].*" ignoreCase="false" />
<conditions>
<add input="{URL}" pattern="(.*)/(admin|desktopmodules|host|tabid)/*" negate="true" />
<add input="{URL}" pattern="^.*\.(xml|ashx|axd|css|js|jpg|jpeg|png|gif)$" negate="true" ignoreCase="true" />
</conditions>
<action type="Redirect" url="{ToLower:{R:0}}" redirectType="Permanent" />
</rule>
For Extensionless and ASPX only to lowercase:
<rule name="LowerCaseRule" stopProcessing="true">
<match url="[A-Z]" ignoreCase="false" />
<action type="Redirect" url="{ToLower:{URL}}" />
<conditions logicalGrouping="MatchAny">
<add input="{REQUEST_FILENAME}" pattern="\.aspx$" />
<add input="{REQUEST_FILENAME}" pattern="\." negate="true" />
</conditions>
</rule>
\.aspx$ matches file names that end with .aspx ($ is end of line)
\. matches anything with a dot in the file name (that wasn't already matched) and negates it from the match

remove .aspx extention using IIS7 & URL Rewrite [duplicate]

I'm using ASP .NET rewriteModule to rewrite http://example.com to http://www.example.com.
<section name="rewriteModule" type="RewriteModule.RewriteModuleSectionHandler, RewriteModule"/>
Then i have this inside <system.webServer>.
<rewrite>
<rules>
<rule name="Canonical" stopProcessing="true">
<match url=".*"/>
<conditions>
<add input="{HTTP_HOST}" pattern="^([a-z]+[.]com)$"/>
</conditions>
<action type="Redirect" url="http://www.{C:0}/{R:0}" redirectType="Permanent"/>
</rule>
</rules>
</rewrite>
Now i want to remove all the .aspx in the end of my pages. Example:
http://www.example.com/Register.aspx
Will turn into:
http://www.example.com/Register/
How can i do that?
I'm on Shared Web Hosting on GoDaddy using IIS7.
These are the standard rewrite rules I start every project with. I use only clean URLs for all the pages (example first rule works for www.example.com/about and second rule www.example.com/product/123)
<rewrite>
<rules>
<rule name="Rewrite default to aspx" stopProcessing="true">
<match url="^$" ignoreCase="false" />
<action type="Rewrite" url="default.aspx" />
</rule>
<rule name="Rewrite page to aspx" stopProcessing="true">
<match url="^([a-z0-9/]+)$" ignoreCase="false" />
<action type="Rewrite" url="{R:1}.aspx" />
</rule>
</rules>
</rewrite>
Pages where I need to parse out the ID (this case number only) and add it to the query string I add a similar rule to the front:
<rule name="Rewrite Product ID" stopProcessing="true">
<match url="^product/([0-9]+)$" ignoreCase="false"/>
<action type="Rewrite" url="product.aspx?id={R:1}"/>
</rule>
If you want to use lower and upper case letters in the URL, set ignoreCase="true"
Edit to answer your second question plus a bonus
This rule will redirect aspx page to the clean URL:
<rule name="Redirect to clean URL" stopProcessing="true">
<match url="^([a-z0-9/]+).aspx$" ignoreCase="true"/>
<action type="Redirect" url="{R:1}"/>
</rule>
Replace url="{R:1}" with url="{ToLower:{R:1}}" to change URL to lowercase. See below why you would want to do this.
Also a good idea to update the Form action so that post backs don't return back to the ugly URL. Using IIS 7.5 or newer this should work:
if (!String.IsNullOrEmpty(Request.RawUrl))
form1.Action = Request.RawUrl;
or for IIS 7:
if (!String.IsNullOrEmpty(Context.Request.ServerVariables["HTTP_X_ORIGINAL_URL"]))
form1.Action = Context.Request.ServerVariables["HTTP_X_ORIGINAL_URL"];
One more thing to keep in mind... it's a good idea to keep all URLs lower case. Mixing lower/upper case characters in the URL creates duplicate content issues for SEO/Google. For example website.com/About and website.com/about will load the same page, but Google will index them as two separate pages.
First you need to remove the .aspx (default.aspx) and redirect to default to change the browser address then add the .aspx and rewire to page using IIS
<rewrite>
<rules>
<clear />
<rule name="Redirect to clean URL" enabled="true" stopProcessing="true">
<match url="^([a-z0-9/]+).aspx$" ignoreCase="true" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Redirect" url="{R:1}" />
</rule>
<rule name="RewriteASPX" enabled="true">
<match url="(.*)" />
<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="{R:1}.aspx" />
</rule>
</rules>
</rewrite>
<rewrite>
<rules>
<remove name="RewriteUserFriendlyURL1" />
<remove name="RedirectUserFriendlyURL1" />
<rule name="RedirectUserFriendlyURL2" stopProcessing="true">
<match url="^www\.myserver\.com/(.*)\.aspx$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
</conditions>
<action type="Redirect" url="www.myserver.com/{R:1}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL2" stopProcessing="true">
<match url="^www\.myserver\.com/(.*)$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="www.myserver.com/{R:1}.aspx" />
</rule>
</rules>
<outboundRules>
<remove name="OutboundRewriteUserFriendlyURL1" />
<rule name="OutboundRewriteUserFriendlyURL2" preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img" pattern="^(.*)www\.myserver\.com/(.*)\.aspx$" />
<action type="Rewrite" value="www.myserver.com/{R:1}" />
</rule>
</outboundRules>
</rewrite>
this will do it - I have generated this vis IIS on my local machine - change myserver.com to your own URL. you can change the regex to actually take care of the x.aspx part of the url then it should work across all pages

Resources