Pass Parameters using <httpRedirect> in ASP.Net - asp.net

In my ASP.Net website I am permanently redirecting an URL to other location using <httpRedirect> in web config.
I am reading URL value from table and redirecting to that URL using Response.Redirect( URL );
It is working perfect.
But now when I try to send the parameter to the calling page using:
Response.Redirect("Default.aspx?name=stackoverflow");
<httpRedirect> in web.config calles the Default2.aspx because of following code in web.config:
<location path="Default.aspx">
<system.webServer>
<httpRedirect enabled="true" destination="Default2.aspx" httpResponseStatus="Permanent" />
</system.webServer>
</location>
The Problem is Default2.aspx does not receive any parameters.
Please help.
Note: I cannot use session variable as page contents depends on that parameter.
For instance,
If user opens another page in new tab with Default.aspx?name=MetaStackOverflow session variable will get replaced and if first page is refreshed then instead of showing Stackoverflow content it will show MetaStackOverflow.

Don't forget the special characters $V$Q and the exactDestination has to be set to True.
<location path="Help">
<system.webServer>
<httpRedirect enabled="true" destination="/Redirect.aspx$V$Q" httpResponseStatus="Permanent" exactDestination="true" />
</system.webServer>
</location>

Related

Forward domain alias to a certain page

We have a domain (domain.com) that has an alias (alias.com). We are using Plesk and a Windows Server. In Plesk, alias.com is setup as an alias for domain.com.
We need that when people access to alias.com it goes to a certain page within the main domain, for example domain.com/this-page.html.
The web site is an ASP.NET MVC web site, in case we can do something using the web.config.
Is this possible? How can we do this?
Open web.config in the directory where the old pages reside
Then add code for the old location path and new destination as follows:
<configuration>
<location path="services.htm">
<system.webServer>
<httpRedirect enabled="true" destination="http://example.com/services" httpResponseStatus="Permanent" />
</system.webServer>
</location>
<location path="products.htm">
<system.webServer>
<httpRedirect enabled="true" destination="http://example.com/products" httpResponseStatus="Permanent" />
</system.webServer>
</location>
</configuration>

IIS Redirect not working without trailing slash

I have a web app that I'm decommissioning and I'm trying to redirect all requests to the old app to a particular page (which shows a link to the replacement and a message advising users to update their bookmarks).
I've set up a HTTP Redirect in the web.config using the answer from this question: ASP.NET httpRedirect : redirect all pages except one and it works except for when the user enters the URL of the site's root folder omitting the trailing slash, in which case it goes to the next directory up the tree, for example:
Original site root: [domain]/foo/bar/
[domain]/foo/bar/specificpage.aspx redirects to [domain]/foo/bar/Default.aspx (OK)
[domain]/foo/bar/ redirects to [domain]/foo/bar/Default.aspx (OK)
[domain]/foo/bar redirects to [domain]/foo/Default.aspx (not OK)
Here's the relevant web.config:
<system.webServer>
<httpRedirect enabled="true" destination="~/Default.aspx" httpResponseStatus="Permanent">
<add wildcard="/" destination="Default.aspx" />
</httpRedirect>
</system.webServer>
<location path="Default.aspx">
<system.webServer>
<httpRedirect enabled="false" />
</system.webServer>
</location>
How can I get it to work when the user goes to [domain]/foo/bar?
For the record, I fixed this by setting the redirect URL to the absolute path of the file, including the virtual directory the site root sits in:
<system.webServer>
<httpRedirect enabled="true" destination="/foo/bar/Default.aspx" httpResponseStatus="Permanent" exactDestination="true">
<add wildcard="/" destination="Default.aspx" />
</httpRedirect>
</system.webServer>
<location path="Default.aspx">
<system.webServer>
<httpRedirect enabled="false" />
</system.webServer>
</location>

Using web.config on a sub-level to redirect users

The application currently has a web.config file at root level and this is shared between all customers. This web config file has the redirects for all customers and every time we modify this web.config for 1 customer, we are putting all other customers at risk.
Each customer has a folder named after them, which is where their theme files and customisations are stored. This location is the ideal place for a web.config file that contains redirects for just that single customer, which can be modified at anytime without putting others at risk.
My current attempts at putting a web.config file at the lowest level looks like it's being read by the server but it fails for some reason. The web config file is very small and only contains the following:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<security>
<requestFiltering>
<fileExtensions>
<remove fileExtension="."/>
<add fileExtension="." allowed="true"/>
</fileExtensions>
</requestFiltering>
</security>
<rewrite>
<rules>
<rule name="test" patternSyntax="Wildcard" stopProcessing="true">
<match url="www.example.com/old_website" />
<action type="Redirect" url="www.example.com/new_website"/>
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
I have my suspicions that it's simply not possible to have a web.config file at such a low level and expect it to work in the same way it does at root level. However, if it is possible and you have an idea for me to try, I would appreciate it.
Thanks,
Craig
It's no problem solving the access the way you try to do it, just add an extra touch to it. :)
I don't know how you handle all the different users but I suppose you have some kind of log in function. When they log in just set some session cookies that you might need for future use. I have a similar solution and I set redirect as a session variable when they log in instead of in the web.config. Then in your subfolder you give only the user logged in access and deny all other users. This look something like this.
<authorization>
<allow users="John"/>
<deny users="*"/>
</authorization>
Here you allow John access to the folder and you deny access to everyone else.
When it comes to error handling I have this in my web.config in root.
<system.web>
<customErrors mode="RemoteOnly" defaultRedirect="Error/Customerror.html"/>
</system.web>
The remote only give errors only on local computor (not on web server) and the default redirect handles any custom error that will arise, also when a user tries to get into a folder they don't have access to. This works like a charm and is tidy.

Redirect dead URLs to new URL in web.config

I have a few (4) links that need to be redirected in a .net website. For example, I need http://www.example.com/products/productname (which now longer exists) to redirect to http://www.example.com/products/productname.aspx.
How can I set up 301 redirects in the web.config?
Thanks!
Currently I have the following:
<location path="draft_root_beer">
<system.webServer>
<httpRedirect enabled="true" destination="~/products/draft_root_beer.aspx" httpResponseStatus="Permanent" />
</system.webServer>
</location>
If it is only adding ".aspx", I suggest you to use URL Rewriting, links will be better for SEO. Otherwise yous something like bellow:
<location path="productname">
<system.webServer>
<httpRedirect enabled="true" destination="http://www.example.com/products/productname.aspx" httpResponseStatus="Permanent" />
</system.webServer>
</location>

Web.Config 301 Redirect

I am trying to redirect a specific page from my old domain to a specific page on my new domain. The URLs look like the following:
http://blog.mysite.com/post/2012/05/hungry.aspx
to
http://mynewsite.com/hungry.aspx
I've looked to the Web.Config file to make this change, however the following code is not working:
<location path="post/2012/05/hungry.aspx">
<system.webServer>
<httpRedirect enabled="true" destination="http://mynewsite.com/hungry.aspx" httpResponseStatus="Permanent" />
</system.webServer>
</location>
When I visit my old blog page, it does no redirection and remains on the old blog page.
Am I missing something?
If http redirection is enabled on old server then you have to put new web config in folder post/2012/05/ with this content
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<httpRedirect enabled="true" destination="http://mynewsite.com/" httpResponseStatus="Permanent" />
</system.webServer>
</configuration>
btw. if all other options fails you could simply do that using Response.Redirect from code behind.

Resources