301 Redirection of website URL - c#

currently we have our website running like this: websitename.com. But we've added multiple languages to it so now all the English content will be available at websitename.com/en and the Russian content will be available at websitename.com/ru. Google has already indexed the URLs at websitename.com. Now, I want to redirect these URLs to default en.
i.e. websitename.com/page1 to websitename.com/en/page1
How do I acheive this?

Redirection is one way to make sure that users always receive the Web
page that they want. Redirection refers to the process of configuring
the Web server to issue a redirect message to the client, such as HTTP
302, which instructs the client to resubmit the request for a new
location. You can redirect users to another file, directory, or site.
You can check MSDN article about IIS Redicrection. Check the links in Procedures section.

Related

Get Azure subdomain name after redirect

I am using azure traffic manager which redirects the url to the relevant server based on traffic.
I want to know where it is been redirected or which subdomian
e.g i am hitting https://example.com and traffic manager redirects it to example.cloudapp.net.
In c# code want to get the subdomain or the redirected url
is it possible.any help would be appreciated.
You can specify a cloud Configuration key 'envNAME' = "MyCloudApp1" for first deployment and "MyCloudApp2" for second cloud service deployment. Then you can directly read that config value and as they differ in each of the environments, you can easily identify that where you request has reached by reading that config value.
There could be other easy solutions as well like looking into your request host headers, but I need to try that.
But the above config based solution should work perfectly.

To Modify the .htaccess for 301 Redirect with Asp.net on the fly (Dynamically)

I make changes using Cpanel / Redirect manager of a Site.
Based on the Changes made on Cpanel for URL redirection, i.e. Say Old Url -> Current Url, I want to modify the .htaccess file dynamically. How do I achieve this using Asp.net?
I assume you want to do URL redirection in ASP.NET. See more information about this topic. http://www.iis.net/downloads/microsoft/url-rewrite.

Check an iFrame is being hosted by an allowed domain

I am developing an iframe for use on a number of our partners websites.
Is there any way I can make sure it can only be used on those websites and not by anyone else
I was intending to add a compulsory querystring to the URL for the website.
Each partner would have a different value in the quesrystring dnd use that to look up an allowed domain
However, is there anyway to know the top level domain of the site hosting the iframe?
Presumably this is not sent in the http request for the iFrame? Or is it, I couldn’t see it?
Or do you need to send the domain from javascript?
Any advice?
However, is there anyway to know the top level domain of the site hosting the iframe?
Nothing reliable.
Presumably this is not sent in the http request for the iFrame? Or is it, I couldn’t see it?
It might be sent in the referer
Or do you need to send the domain from javascript?
If you want to fetch it from the framed page, you will be blocked by the same origin policy.
If you want to sent it from the framing page, you will be putting it in the query string and you can't trust it because it can be set to whatever the person writing the framing page likes.
There is also the X-Frame-Options header (but that has limited browser support).
The most reliable solution I can think of is:
Require the origin to be specified in the query string used to load the frame
Check the referer. If it doesn't match your white-list and is not blank, redirect to a page that is blank except for a link to your site with target="_top" and some JavaScript that top.location = "your site"
Check that the origin specified in the query string is on your whitelist, if it isn't act in the same way as a rejected step 2
Output an X-Frame-Options header that limits the framing to the specified origin
That is likely to catch enough browsers to discourage the framing site from framing your site.
You can try to check referrer which normal browser will send for IFrame requests on the page.
You also can use "x-frame-options" header covered in (How to Block Iframe call and MDN ) but not every browser will respect that (on other hand it is more reliable if browser supports it).
iframe's sanbox attribute might be helpful in controlling the various security aspects in an iframe including origins
http://www.html5rocks.com/en/tutorials/security/sandboxed-iframes/
The Architecture Journal of 2007 has a nice article about this: Secure Cross-Domain Communication in the Browser
Basically what the article suggests is:
If you have page A on domain 1 with an iframe with page B on domain 2 as its source , then having an iframe on page B to page C on domain 1, would allow you to pass information across domains
I haven't tested it, but this sounds like it could work.
Another possibility is create a file with a special filename (for instance a hash of the URL of page B on domain 2) and basic extension (like .htm) and place it in the root of domain 1. Checking whether the file exists on domain 1 cannot be done by javascript however, so it should be done with server side code.

Check if redirected to default page

How to check using C# if "redirect" to "default document" happened?
For example, in browser I type URL: mysite.com/. When on server I check HttpContext.Current.Request.Url.AbsoluteUri, i receive mysite.com/default.aspx...
How I can get the exact URL that user has in his browser?
Thanks
EDIT: After some questions about the needs, I will give more details.
I have page with default.aspx with iframe inside of it. The iframe src is not the same origin (default.aspx is http and iframe content is https). On server side, i need to set the query string param to the src of iframe to include the exact URL that user has in browser. I need it in order to be able to set parent.location = parentURL + '#myparam' on iframe client side.
Currently everithing works fine, except when the request made to domain name without providing file name.
Try HttpContext.Current.Request.RawUrl
You typed
mysite.com/.
and you get
mysite.com/default.aspx...
Because you have set default.aspx as the default / Start up page in your site. The browser always redirect to the default page. I think when we type mysite.com the asp.net automatically appends the default page in the URL, so when we use Request.Url we get the mysite.com/default.aspx
Reading your intention of the IFrame, perhaps you are looking for Framset Script to determine the redirection ?
if (parent.location.href==window.location.href)
{
// you re-direction codes...
}
EDIT :
Giving a different HTTP and HTTPS, it's likely the Same Origin Policy kicked in. There is a workaround you could use PostMessage interface for cross sites.
Other option would be managed by Server(IIS) so that both http/https url request setting to default document , so that you don't need to alter client-side scripting for such complication handling.
You should delete 'Default.aspx' page from your IIS Default document list. then you get exact URL that user entered.

URL rewriting and html canonical link in the header to avoid duplicate page issue for search engines

I am using Intelligenicai.URLRewriter.dll to rewrite my website URL it is working fine i followed the following link as an example
http://anupkumargupta.wordpress.com/2011/05/28/url-rewriting-with-urlrewriter-net-simplest-way-asp-net-c-sql-blog/
On the other article i read about URL rewrite can create problems for search engines as now we can access same page with two different URL one without URL rewrite and one with URL rewrite. (Check this link http://thecodebug.com/?p=296)
Above mentioned link talks about search engines can penalize if they find duplicate contents on the website. This article (http://thecodebug.com/?p=296) talks about "html canonical link in the header" if we don't add them then we can tell search engine that this url is canonical.
Now my question is this applicable to even if i use Intelligenicai.URLRewriter.dll and if this is the fact what are the precautions i should take to avoid such scenario.
Example of my URL
With URL Rewrite
http://www.xyz.com/Article/en-US/19/87/let-the-spirit-of-our-nation’s-founders-guide-us.aspx
URL without rewrite
http://www.xyz.com/Article/ArticleDetails.aspx?Language=en-US&PageID=19&ArticleID=87
Part of web.config
<rewriter>
<rewrite url="~/Article/(.+)/(.+)/(.+)/(.+).aspx" to="~/ArticleDetails.aspx?Language=$1&PageID=$2&ArticleID=$3" processing="stop"/>
</rewriter>
You can add 2 rules: One rewriting the new-style links to the physical URLs then stop processing rules, and then follow it with a rule to rewrite direct requests to the aspx page to the new style URL, 301 permanent redirect, and stop processing rules. The 301 redirect is important! If you can't get it to do that with your rewriting DLL then you can redirect the URL to a special hidden page that will accept the old page as the querystring or looking at the referrer, then mapping it out to the new page using a 301 redirect there.
UPDATE: Here's some more information:
Using Intelligenica UrlRewriter, you would set up two rules, in this order:
The first rule uses the rule you have already set up for redirecting the fancy URL to the physical file.
The second rule takes the path to the physical file and either (a) 404 or (b) 301 permanent redirects to the fancy URL. If you have a single page serving up multiple pages of content, you may be better off just stopping processing with a 404 error. Also, as long as you never use the old-style links anywhere in your site, you should be okay. If at ANY TIME you used the old style links (before you rewrote the URLs), you definitely need to make sure that you have rewriting set up to redirect or 404 the page so the new style URLs are forced.
<rewriter>
<rewrite url="~/Article/(.+)/(.+)/(.+)/(.+).aspx" to="~/ArticleDetails.aspx?Language=$1&PageID=$2&ArticleID=$3" processing="stop"/><!-- rewrites URL -->
<rewrite url="~/ArticleDetails.aspx?Language=(.+)&PageID=(.+)&ArticleID=(.+)" to="~/Article/$1/$2/$3.aspx" processing="stop" permanent="true" /><!-- Redirects old page to new url with 301 -->
</rewriter>
Hope this helps.

Categories

Resources