URL Rewriting Not work in Asp.net 4.0 on IIS7

Posted by Rahul_Shinde under .NET Framework on 9/20/2013 | Points: 10 | Views : 7730 | Status : [Member] | Replies : 23
1. The current home page url must be
changed from http://10.1.0.15/JetPortal/Home/Homepage.aspx to
http://10.1.0.15/JetPortal

2. In all other urls like

About : http://10.1.0.15/JetPortal/Common/About_Us.aspx
Service : http://10.1.0.15/JetPortal/Common/Our_Services.aspx
Career : http://10.1.0.15/JetPortal/Common/Careers.aspx
Contact Us :http://10.1.0.15/JetPortal/Common/Contact_Us.aspx

Removal of "common" from the above
URL



So, Please Advoice how can we achive the same .

we have already tried from IIS7 and Web.config ,but its not working

please check belows code and advoice.


<rewrite>

<rule>
<rule name="Rewrite to folder1" stopProcessing="true">
<match url="http://10.1.0.15/JetPortal/Home/Homepage.aspx" />
<conditions>
<add input="{HTTP_HOST}" pattern="^http://10.1.0.15/JetPortal/Home/Homepage.aspx$" /
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" url="Common/{R:1}" />
</rule>
<rule name="Rewrite to folder2" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^http://10.1.0.15/Home/JetPortal/Homepage.aspx$" />
</conditions>
<action type="Rewrite" url="Common/{R:1}" />
</rule>-

</rule>




Responses

Posted by: Bandi on: 9/20/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Refer this link
http://www.iis.net/learn/extensions/url-rewrite-module/iis-url-rewriting-and-aspnet-routing
http://www.iis.net/learn/extensions/url-rewrite-module/using-rewrite-maps-in-url-rewrite-module

Solution:
http://forums.iis.net/t/1178719.aspx

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Rahul_Shinde, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rahul_Shinde on: 9/20/2013 [Member] Starter | Points: 25

Up
0
Down
I have tried it but showing error
The Web server is configured to not list the contents of this directory.

for this code
<rewrite>
<rules>
<rule name="Deafult Document URL Rewrite" stopProcessing="true">
<match url="(.*?)/?Homepage\.aspx$" />
<action type="Redirect" url="{R:1}/" />
</rule>
</rules>
</rewrite>

Please advoice it's urgent........................

Rahul_Shinde, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/20/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
refer the following rules to rewrite URL
There should be a difference between URLs you need to rewrite and those you don't, otherwise the rule will capture some wrong URLs.

This rule will pick up the first part of the page-name only:


^cc/(\w+)-\w+-\w+-\w+\.aspx

1. The page-name must be in the pattern abc-def-ghi-jkl.aspx

2. The ^ at the start means that after the domain the URL must begin with the 'cc' directory, e.g. www.asd.com/cc/etc...

3. {R1} will equal 'abc'

4. The rule will NOT rewrite www.asd.com/test-url-name.aspx, because it does not contain the cc directory


visit http://www.iis.net and search for URL Rewriting to see some tutorials.
Refer http://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Rahul_Shinde, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/20/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Refer this link
http://www.iis.net/learn/extensions/url-rewrite-module/url-rewrite-module-configuration-reference#Redirect_action

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Rahul_Shinde, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rahul_Shinde on: 9/20/2013 [Member] Starter | Points: 25

Up
0
Down
I have tried the below code in config but it showing error

The Web server is configured to not list the contents of this directory

<rule name="Default Document URL Rewrite" stopProcessing="true">
<match url="(.*?)/?/Homepage\.aspx$" />
<action type="Redirect" url="{R:1}/" logRewrittenUrl="true" />
</rule>

please advoice ................

Rahul_Shinde, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/20/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Take help from these links
http://stackoverflow.com/questions/10601115/efficient-url-masking-cloacking-hiding-in-existing-asp-net-4-solution
http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx
http://urlrewriter.net/

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Rahul_Shinde, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rahul_Shinde on: 9/20/2013 [Member] Starter | Points: 25

Up
0
Down
I have tried all of the above solution but still getting same error



Please advoice...............

Rahul_Shinde, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rahul_Shinde on: 9/23/2013 [Member] Starter | Points: 25

Up
0
Down
I have tried it but showing error
The Web server is configured to not list the contents of this directory.

for this code
<rewrite>
<rules>
<rule name="Deafult Document URL Rewrite" stopProcessing="true">
<match url="(.*?)/?Homepage\.aspx$" />
<action type="Redirect" url="{R:1}/" />
</rule>
</rules>
</rewrite>

Please advoice it's urgent........................

Rahul_Shinde, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/23/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Refer this link to redirect to URL by removing folder name
http://stackoverflow.com/questions/18436753/how-to-remove-folder-name-from-url-in-asp-net
http://forums.iis.net/t/1201212.aspx

http://www.hanselman.com/blog/RedirectingASPNETLegacyURLsToExtensionlessWithTheIISRewriteModule.aspx

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Rahul_Shinde, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rahul_Shinde on: 9/23/2013 [Member] Starter | Points: 25

Up
0
Down
How to get above with the help of global.asax file



please advoice..........

Rahul_Shinde, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/23/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
URL Rewriting in global.asax page:
http://maulikpshah.wordpress.com/2010/03/31/rewrite-url-using-global-asax/
http://www.dotnetperls.com/rewritepath

Sample code
To rewrite the url only using asax file like www.domain.com/items.aspx?ID=1
to www.domain.com/fackurl.aspx?ID=1 Add the below content in the Global.asax file
protected void Application_BeginRequest(object sender, EventArgs e)

{
HttpContext myContext = HttpContext.Current;
Regex rewrite_regex = new Regex(@"(.+)\/((.+)\.aspx)", RegexOptions.IgnoreCase);
try
{
//see if we need to rewrite the URL
Match match_rewrite = rewrite_regex.Match(myContext.Request.Path.ToString());
if (match_rewrite.Groups[2].Captures[0].ToString() == "Default.aspx")
{
if (Request.QueryString["ID"] != null)
myContext.RewritePath("~/Mail/Index.aspx?ID=" + Request.QueryString["ID"].ToString(), true);
}
}
catch (Exception ex) { }
}


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Rahul_Shinde, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/23/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Using ASP.NET MVC with Different Versions of IIS (C#)
http://www.asp.net/mvc/tutorials/older-versions/deployment/using-asp-net-mvc-with-different-versions-of-iis-cs

URL Rewriting and trailing extensions:
http://www.abdullahnoor.com/techblog/url-rewriting

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Rahul_Shinde, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rahul_Shinde on: 9/23/2013 [Member] Starter | Points: 25

Up
0
Down
It's also doesent work.
Please give another way................

Rahul_Shinde, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/23/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Can you elaborate your exact scenario... It might help you and me

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Rahul_Shinde, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rahul_Shinde on: 9/23/2013 [Member] Starter | Points: 25

Up
0
Down
Please see the belows eg. here i want url

http://localhost:57011/TestURL/Default.aspx

instate of

http://localhost:57011/TestURL/Home/Default.aspx this

please help........

<?xml version="1.0" encoding="UTF-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<connectionStrings>
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<authentication mode="Forms">
<forms loginUrl="~/Account/Login.aspx" timeout="2880" />
</authentication>
<membership>
<providers>
<clear />
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
</providers>
</membership>
<profile>
<providers>
<clear />
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
</providers>
</profile>
<roleManager enabled="false">
<providers>
<clear />
<add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />
<add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
</providers>
</roleManager>
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<rewrite>
<rules>
<rule name="Deafult URL" stopProcessing="true">
<match url="(.*?)/?Default\.aspx$" />
<action type="Redirect" url="{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>




Rahul_Shinde, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rahul_Shinde on: 9/23/2013 [Member] Starter | Points: 25

Up
0
Down
Please see above senario and give solution.

Please see the belows eg. here i want url

http://localhost:57011/TestURL/Default.aspx

instead of

http://localhost:57011/TestURL/Home/Default.aspx this

how i can achive this

Rahul_Shinde, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/23/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Follow this procedure for IIS URL Rewrite – rewriting non-www to www
http://weblogs.asp.net/owscott/archive/2009/11/27/iis-url-rewrite-rewriting-non-www-to-www.aspx

In the same way follow the above steps for User-friendly URLs by using "URL Rewrite" in the sites of IIS Manager...
Then you can found script of URL Rewrite in the web.config file .....


Support links are:
http://stackoverflow.com/questions/9302247/iis7-url-rewriting-outbound-rules?lq=1
IIS 7.5 URL Rewrite - Rewrite a Folder from an URL
http://stackoverflow.com/questions/10615572/iis-7-5-url-rewrite-rewrite-a-folder-from-an-url?rq=1
create seo friendly url in .net
http://techathon.mytechlabs.com/create-seo-friendly-url-in-net/

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Rahul_Shinde, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/23/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
- If a request is http://mysite.co/LegacySite/page.aspx the URL will be rewritten to http://mysite.co/page.aspx

The following should work:
<rule name="MyRole" stopProcessing="true">

<match url="LegacySite/(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^mysite.com$" />
</conditions>
<action type="Rewrite" url="/{R:1}" appendQueryString="true" />
</rule>

You might want to drop the conditional for checking the host name. Is that really important? Do you have any other domain names bound to that website for which you don't want the redirect to happen? It seems unnecessary. You probably only need:

<rule name="MyRole" stopProcessing="true">

<match url="LegacySite/(.*)" />
<action type="Rewrite" url="/{R:1}" appendQueryString="true" />
</rule>

I've added appendQueryString="true" to pass any (optional) query string parameters to the rewritten URL.


Refer this link: http://stackoverflow.com/questions/13030056/iis-7-5-url-rewrite-rule-higher-priority-than-folder?rq=1

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Rahul_Shinde, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rahul_Shinde on: 9/24/2013 [Member] Starter | Points: 25

Up
0
Down
Dear Friends....
Let me clear u all again..

1)I have just added new website.
2)In that i have created one folder name Home.
3)Inside that folder i have added Homepage.aspx page.
5)When i run the Homepage.aspx from IIS it shows the url

http://10.1.0.15/Jetcar/Home/Homepage.aspx

6)But i want to rewrite this url as

http://10.1.0.15/Jetcar/Homepage.aspx

i want to remove the folder name..

7) Also i have 1 index.html page.

index.html code:

<script language="javascript" type="text/javascript">
window.location = "http://10.1.0.15/Jetcar/Home/Homepage.aspx";

</script>

in index.html i have set the path which comes first as an output when i run the project.

8) for this i have done the changes in web.config file also..
web.config Code

<rule name="MyRole" stopProcessing="true">

<match url="Home/(.*)" />

<action type="Rewrite" url="/{R:1}" appendQueryString="true" />

</rule>


Still it shows the error

The resource cannot be found.

and URL is Same http://10.1.0.15/Jetcar/Home/Homepage.aspx


please give me the solution for this ...................



Rahul_Shinde, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rahul_Shinde on: 9/27/2013 [Member] Starter | Points: 25

Up
0
Down

Please see the belows code for Test project :

I want URL http://localhost:2735/TestURL/Default.aspx

instead of http://localhost:2735/TestURL/Home/Default.aspx

I want to Remove Home folder from URL ,

so please tell me what code should i required write in

web.config inside Rewrite Rule.




Rahul_Shinde, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/27/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Refer this once...
web.config rewrite exclude directory
http://stackoverflow.com/questions/7299821/web-config-rewrite-exclude-directory


To hide the directory in url ( Refer Post at Dec 16, 2011 02:03 AM ) in the following link
http://forums.iis.net/t/1184035.aspx

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Rahul_Shinde, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rahul_Shinde on: 9/28/2013 [Member] Starter | Points: 25

Up
0
Down
I have already Refered the above but not getting result

Rahul_Shinde, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rahul_Shinde on: 10/8/2013 [Member] Starter | Points: 25

Up
0
Down
I have done this but only for Home page .
i also have one other folder ie.Common that i want to Remove

Please advice .............

Rahul_Shinde, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response